对 NSArray 进行排序,忽略变音符号

发布于 2024-09-30 17:45:05 字数 124 浏览 0 评论 0 原文

我想对我拥有的数组进行排序,以便将变音符号视为字符的正常版本(ä == a 等)。我认为 localizedCaseInsensitiveCompare: 应该可以解决问题。但事实并非如此。有人吗?

最好
–f

I want to sort an array I have so that the Umlauts are treated as normal versions of the characters (ä == a, etc.). I thought localizedCaseInsensitiveCompare: should do the trick. But it doesn't. Anyone?

Best
–f

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-10-07 17:45:05

localizedCaseInsensitiveCompare: 使用用户选择的当前语言的标准规则进行比较,忽略大小写。在你的情况下,你想忽略变音符号,而不是情况。这意味着您需要做其他事情。

您需要使用 compare:options: 并将 NSDiacriticInsensitiveSearch 作为选项传递。请参阅这里

要使用它对数组进行排序,您需要使用 sortedArrayUsingComparator: 使用块,或者在 NSString 中实现类别方法并将该选择器传递给 sortedArrayUsingSelector:< /代码>。不要忘记为类别方法的名称添加前缀,这样它就不会与框架中的私有方法重叠。所以,做一些类似的事情

@interface NSString (myaddition)
-(NSComparisonResult)mySecretDiacriticsInsensitveCompare:(NSString*)string;
@end

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 pass NSDiacriticInsensitiveSearch 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 in NSString and pass that selector to sortedArrayUsingSelector:. 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

@interface NSString (myaddition)
-(NSComparisonResult)mySecretDiacriticsInsensitveCompare:(NSString*)string;
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文