2012-02-03 64 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