使用特殊字符对数组进行排序 - iPhone
我有一个带有法语字符串的数组,可以说:“égrener”和“exact”我想对其进行排序,例如 égrener 是第一个。当我这样做时:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
我在列表末尾看到 é...我该怎么办?
谢谢
I have an array with french strings let say: "égrener" and "exact" I would like to sort it such as égrener is the first. When I do:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
I get the é at the end of the list... What should I do?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSString
中有一个方便的方法,可以让您轻松地进行这种类型的排序:NSString
的底层比较方法 (< code>compare:options:range:locale:) 为您提供了更多关于如何排序的选项。编辑:这是一个长篇故事:
首先,定义一个比较函数。这对于自然字符串排序很有用:
然后,对数组进行排序。
示例中的数组包含一些有趣的字符:数字、变音符号以及 unicode 范围 ff00 中的一些字符。最后一种字符类型看起来像 ASCII 字符,但以不同的宽度打印。
使用的比较函数以人类可预测的方式处理所有情况。排序后的数组具有以下顺序:
There’s a convenience method in
NSString
that lets you do this type of sorting easily:NSString
’s underlying comparison method (compare:options:range:locale:
) gives you even more options of how sorting should be done.Edit: Here’s the long story:
First, define a comparison function. This one is good for natural string sorting:
Then, sort the array.
The array in the example contains some funny characters: Numbers, diacritical marks, and some characters from unicode range ff00. The last character type looks like an ASCII character but is printed in a different width.
The used comparison function handles all cases in human predictable way. The sorted array has the following order: