2012-02-03 58 views
2

我有一个NSArray,我想按字母顺序排序,我目前使用sortedArrayUsingSelector:@selector (localizedCaseInsensitiveCompare:)。返回NSArray排序问题与首字母缩略词排序第一

CAA 
CBL 
CCC 
Cab 
Car 
Cat 

首字母缩略词首先按字母顺序排列,其次是其他字母。我会用什么来排序,以便首字母缩写词与常规单词一起排序?似乎无法在Apple的文档中找到答案。

book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

回答

0

您可以使用

- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr 

并创建NSComparator所期望的行为。

像这样:

NSarray *sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) { 
    //Decide in the order 
    if (yourCondition) 
    { 
    return NSOrderedSame 
    } 
}]; 
+0

感谢奥斯卡。你能详细说明一下吗?我的完整代码是'book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare :)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];' – awDemo 2012-02-03 20:43:28

+0

感谢您的编辑你的答案...我从来没有使用NSComparator,所以我需要解决这个问题,看看它是否有效。我会告诉你。我很惊讶为什么首字母缩略词是先捆在一起的...... – awDemo 2012-02-03 21:25:45