像 MPMediaPickerController/iPod 库一样对 NSArray 进行排序
我正在为 iPhone 开发一个自定义 UIViewController,它模拟应用程序本地文档目录中文件的 MPMediaPickerController 子集。特别是,我正在尝试重新创建“歌曲”选项卡。我已经成功创建了我的新控制器,但我无法像在 iPod Library 或 MPMediaPickerController 中那样对歌曲标题进行排序。以下是如何对歌曲名称进行排序的示例:
- Awesome Song Title
- Cool Song
- The Darkest Song Ever
- 我的歌曲标题
- A Very Cool Song
- 为什么是我?
- 浪费 4 小时
如您所见,排序排除了歌曲标题中的主要文章,并将以数字值开头的歌曲放在列表末尾。谁能建议一个考虑这些图的有效排序函数?
I'm developing a custom UIViewController for iPhone that emulates a subset of the MPMediaPickerController for files in my application's local documents directory. In particular, I'm attempting to re-create the Songs tab. I've been successful in creating my new controller, except I can't get the song titles to sort like they do in the iPod Library or the MPMediaPickerController. Here's an example of how the song names need to be sorted:
- Awesome Song Title
- Cool Song
- The Darkest Song Ever
- My Song Title
- A Really Cool Song
- Why Me?
- 4 Hours Wasted
As you can see, the sorting excludes the leading articles in the song titles and also places songs that begin with a numeric value at the end of the list. Can anyone suggest an efficient sorting function that takes these tules into account?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于看起来没有人可以提供解决方案,我想我会发布我想出的解决方案。首先,我为数据创建了一个模型:
模型的关键是ComparableString 方法,该方法用于创建sortableValue。以下是该模型的实现:
除了从字符串中删除前导文章之外,它还删除所有前导非字母字符。我的 iPod 库中有一首名为“$ell Your $oul”的歌曲,它最终位于 MPMediaPickerController 的 E 部分。我不确定如果我创建了初始排序算法,我会这样做,但我要与 MPMediaPickerController 保持一致,所以就这样吧。
最后一块拼图是 UILocalizedIndexedCollation 类。这个方便的小帮助器类将帮助您对数据进行排序,从而通过 UITableViewDataSource 将其提供给 UITableView 变得轻而易举。以下是有关如何将 UILocalizedIndexedCollation 类与模型结合使用的片段:
关于 UILocalizedIndexedCollation 的一个快速说明(来自 Apple 的文档):
因此,请确保为您想要支持的每种语言提供 Localized.strings,否则您的表视图将只有 AZ 和 # 部分。
我花了一段时间才弄清楚这方面的所有细节,所以我希望它对其他人有用。如果您发现我可以改进这一点的任何方法,请告诉我!
Since it looks like no one could offer up a solution, I thought I would post the solution I came up with. First, I created a model for my data:
They key to the model is the comparableString method, which gets used to create the sortableValue. Here's the implementation of the model:
In addition to the removing the leading articles from the string, it also removes any leading non-letter characters. I have a song in my iPod library entitled "$ell Your $oul", which ends up in the E section in the MPMediaPickerController. I'm not sure that's what I would have done had I crated the initial sorting algorithm, but I was going to consistency with the MPMediaPickerController, so there you go.
The final piece of the puzzle is the UILocalizedIndexedCollation class. This handy little helper class will help you sort your data to make supplying it to a UITableView via a UITableViewDataSource a piece of cake. Here's a snippet on how to use the UILocalizedIndexedCollation class in conjunction with the model:
One quick note about UILocalizedIndexedCollation (from Apple's documentation):
So make sure you provide a Localizable.strings for each language you want to support, or your table view will only have sections A-Z and #.
It took me a while to work out all of the details on this, so I hope it becomes useful for other people. If you see any ways I can improve this, please let me know!
您可能还需要考虑某些带有重音符号的字符,例如 è、é、ò、à、ù、ì。
所以我稍微修改了你的代码以合并这一点。你的代码是对我们所有 iPhone 开发者的巨大贡献
You may need to consider certain characters with accents as well such as è, é, ò, à, ù, ì.
So I slightly modified your code to incorporate this. Your code is a great contribution to all of us iphone developers