对 NSArray 进行排序,忽略变音符号
我想对我拥有的数组进行排序,以便将变音符号视为字符的正常版本(ä == a 等)。我认为 localizedCaseInsensitiveCompare: 应该可以解决问题。但事实并非如此。有人吗?
最好
–f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想对我拥有的数组进行排序,以便将变音符号视为字符的正常版本(ä == a 等)。我认为 localizedCaseInsensitiveCompare: 应该可以解决问题。但事实并非如此。有人吗?
最好
–f
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
localizedCaseInsensitiveCompare:
使用用户选择的当前语言的标准规则进行比较,忽略大小写。在你的情况下,你想忽略变音符号,而不是情况。这意味着您需要做其他事情。您需要使用
compare:options:
并将NSDiacriticInsensitiveSearch
作为选项传递。请参阅这里。要使用它对数组进行排序,您需要使用
sortedArrayUsingComparator:
使用块,或者在NSString
中实现类别方法并将该选择器传递给sortedArrayUsingSelector:< /代码>。不要忘记为类别方法的名称添加前缀,这样它就不会与框架中的私有方法重叠。所以,做一些类似的事情
localizedCaseInsensitiveCompare:
compares using the standard rules in the current language chosen by the user, ignoring the case. In your case you want to disregard the diacritics, not the case. This means you need to do something else.You need to use
compare:options:
and passNSDiacriticInsensitiveSearch
as an option. see here.To sort an array using it, you need to either use a block using
sortedArrayUsingComparator:
, or implement a category method inNSString
and pass that selector tosortedArrayUsingSelector:
. Don't forget to prefix the name of the category method so that it doesn't overlap with a private method in the framework. So, do something like