您可以使用comparator blocks。
在你的情況下,該塊應該做以下幾點:
得到第一個字母,使用[obj1 substringToIndex:1]
這一理念的完整代碼:
NSArray *array = [NSArray arrayWithObjects:@"peach",@"apple",@"banana",@"ananas",@"50cents", @"papaya", nil];
array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *s1 = [obj1 substringToIndex:1];
NSString *s2 = [obj2 substringToIndex:1];
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location) {
return [obj1 compare:obj2];
} else {
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound) {
return NSOrderedAscending;
}
}
return NSOrderedDescending;
}];
當您使用sortUsingDescriptors:
,你必須使用的NSMutableArray。您可以就地使用與比較排序與sortUsingComparator:
。
[array sortUsingBlock:<The block stays the same>];
http://stackoverflow.com/questions/8242735/how-to-sort-array-controller-alphabetically-with-numbers-last-in-objective-c –