Objective c NSArray排序问题,首字母缩略词优先排序
我有一个NSArray,我想按字母顺序排序,我目前正在使用Objective c NSArray排序问题,首字母缩略词优先排序,objective-c,ios,cocoa-touch,nsarray,Objective C,Ios,Cocoa Touch,Nsarray,我有一个NSArray,我想按字母顺序排序,我目前正在使用sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)。这是回报 CAA CBL CCC Cab Car Cat 首字母缩略词先按字母顺序排列,然后按其他字母顺序排列。我将使用什么来排序,以便首字母缩略词与常规单词一起排序?在苹果的文档中似乎找不到答案 book=[[self.sections valueForKey:[[self.sections allke
sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)
。这是回报
CAA
CBL
CCC
Cab
Car
Cat
首字母缩略词先按字母顺序排列,然后按其他字母顺序排列。我将使用什么来排序,以便首字母缩略词与常规单词一起排序?在苹果的文档中似乎找不到答案
book=[[self.sections valueForKey:[[self.sections allkey]sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexath.section]]objectAtIndex:indexath.row]
您可以使用
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
并为所需的行为创建NSComparator
像这样:
NSarray *sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) {
//Decide in the order
if (yourCondition)
{
return NSOrderedSame
}
}];
你可以用
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
并为所需的行为创建NSComparator
像这样:
NSarray *sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) {
//Decide in the order
if (yourCondition)
{
return NSOrderedSame
}
}];
谢谢你,奥斯卡。你能详细说明一下吗?我的完整代码是
book=[[self.sections valueForKey:[[self.sections allkey]sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexath.section]]objectAtIndex:indexath.row]代码>感谢您编辑您的答案。。。我从来没有使用过Ncomparator,所以我需要解决这个问题,看看它是否有效。我会让你知道的。我很惊讶为什么首字母缩略词会首先捆绑在一起…谢谢奥斯卡。你能详细说明一下吗?我的完整代码是book=[[self.sections valueForKey:[[self.sections allkey]sortedarrayingselector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexath.section]]objectAtIndex:indexath.row]代码>感谢您编辑您的答案。。。我从来没有使用过Ncomparator,所以我需要解决这个问题,看看它是否有效。我会让你知道的。我很惊讶为什么首字母缩略词首先被捆绑。。。