如何在我的iPhone应用程序中对Unicode(外部)字符串进行排序?排序Unicode字符串
回答
感谢您的帮助。
localizedCaseInsensitiveCompare
这个API将做排序相对于本地化。否则,不同语言的字符串将被视为unicode字符串,并且比较失败。
这里是解决方案:
NSComparisonResult sortLocationsForStr(NSString* str1, NSString* str2, void *context)
{
return [str1 localizedCaseInsensitiveCompare:str2];
}
[myArrayToSort sortUsingFunction:sortLocationsForStr context:nil];
Objective-C(NSString *)中的所有字符串都是unicode字符串。它们是unicode字符序列(与编码字节序列相反)。您可以使用NSArray提供的方法对字符串数组进行排序。
NSArray* myArray = [NSArray arrayWithObjects:@"はじめまして", @"русский язык", @"คนอ้วน ๆ", nil];
NSArray* mySortedArray = [myArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
这将通过它们的Unicode顺序,如果你有一些其他的排序条件,那么提供一个自定义排序选择或详细说明你的问题的字符串进行排序。
你是什么意思 “在它们的Unicode秩序”?你的意思是按Unicode排序算法定义的顺序?这将是Unicode顺序。如果您的意思是代码点顺序,那是纯粹的垃圾并且不会产生字母排序。 – tchrist 2011-08-16 01:38:27
@tchrist:+1。我同意排序点顺序是没有意义的。然而,这个问题根本没有提到“按字母排序”。对于我所知道的他们想要一个任意排序的二进制搜索数组?我鼓励他们详细阐述他们的问题,我会详细阐述我的答案。也许你可以创建一个解释如何在Objective-C中使用Unicode排序算法的答案? – Akusete 2011-08-16 03:05:13
如果你想使用排序描述符进行排序,也可以进行排序,像这样:
NSSortDescriptor *sorter = [NSSortDescriptor descriptorWithKey:@"description" ascending:YES];
NSArray* mySortedArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorter]];
“描述”工程确定为NSString的值的关键,因为描述记录在案,以返回其自身。
使用NSSortDescriptor路由的优点是您可以有多个排序描述符,或者您可以选择使用降序排序。
如果你想用一个排序描述符对一个NSString数组进行排序,最好使用'self'作为关键字。 http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/doc/uid/20000052-BBCGEBBJ – JeremyP 2010-08-02 05:30:24
for NSString,“description”相当于它总是返回NSString对象本身。我认为它比“self”更容易混淆,它可能会被误解为调用类而不是数组中的对象...... – 2010-08-02 16:21:48
- 1. Unicode字符串为Unicode字符,Python 3
- 2. 排序字符串
- 3. Unicode字符字符串
- 4. 搜索和排序不工作的Unicode字符串
- 5. QPython unicode字符串
- 6. Unicode字符串++/CLI
- 7. Unicode字符串php
- 8. len(unicode字符串)
- 9. raw_inputting Unicode字符串
- 10. Qpython unicode字符串
- 11. 拆分字符串,unicode,unicode,python中的字符串
- 12. Unicode字符串文字
- 13. 支持Unicode的字符串(1)程序
- 14. SQL字符串排序
- 15. mySQL按字符串排序
- 16. Groovy排序字符串asc
- 17. 排序字符串列表
- 18. Javascript字符串排序
- 19. 排序字符串数组
- 20. C# - 排序字符串ArrayList
- 21. 排序字符串数组
- 22. 排序分配字符串
- 23. 字符串矢量排序
- 24. 字符串排序问题
- 25. SQL按字符串排序
- 26. 字符串排序队列
- 27. JSON排序字符串
- 28. 排序UTF-8字符串?
- 29. 字符串排序CSV行
- 30. 字符串按字符数排序
您需要使用Unicode归类算法。 – tchrist 2011-08-16 01:39:01