开发者

Sort NSArray containing åäö

Need help to sort NSArray that contains åäö (Swedish chars) I am using for the moment

[keyArray addObjectsFromArray:[[names allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; 

But this is sort A Å Ä开发者_运维百科 Ö


This question is similar to the one I answered here Sort NSArray of NSStrings like Addressbook on iphone sort . You need to do a diacritic insensitive search.

NSArray *array = [NSArray arrayWithObjects:@"aar", @"åäö", @"aao", nil];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];

Note: sortedArrayUsingComparator: requires iOS 4.0 and above. For iOS < 4.0 use sortedArrayUsingSelector:.


As the comments below the question and the answer states; [NSString localizedCompare:] is the best way to get words with åäö sorted the right way.

As I understand it NSDiacriticInsensitiveSearch treats å and ä as a and ö as o and that is not what you want when sorting swedish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜