2011-04-04 64 views
70

我有關於數組排序WRT數據庫中的問題排序的數組:我想用NSSortDescriptor

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; 
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; 

[mGlossaryArray sortUsingDescriptors:sortDescriptors]; 
[sorter release]; 

在這裏,在數據庫中有一些第一個大寫字母,因爲大寫字母不告訴我適當的排序輸出。在這裏,我正在用r.t「w」對數組進行排序,這是我在數據庫中的表列。 在這裏,我附上了輸出的屏幕截圖,其中說「癌症」比「c」更先出現,但這是不正確的,因爲大寫字母沒有按字母順序排序。

例如。如果小寫字母和「aCid」中有「able」,那麼它會首先顯示一個cid,然後才能顯示,並且還有一種情況,如果第一個字母是首字母大寫,例如「Able」和「a」。這裏Able首先顯示。 enter image description here

回答

166

到這裏看看: Creating and Using Sort Descriptors

可以作爲不區分大小寫的比較。

NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] 
      initWithKey:@"w" 
      ascending:YES 
      selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; 
[mGlossaryArray sortUsingDescriptors:sortDescriptors]; 
+0

但它仍然沒有排序它首先給我所有的資本,然後小寫 – 2011-04-04 19:01:32

+0

你可以給你一些你想要的樣品嗎? – 2011-04-04 19:21:17

+0

看看圖像顯示器,它給了我未排序的輸出,就像在這種情況下,「c」應該首先出現,但是癌症出現在第一個位置。所以請在這種情況下幫助我。 – 2011-04-04 19:30:42

2

我認爲這樣做對你有用。它的文檔在這裏: String Programming Guide

添加這個蘋果寫的小函數。

int finderSortWithLocale(id string1, id string2, void *locale) 
{ 
    static NSStringCompareOptions comparisonOptions = 
     NSCaseInsensitiveSearch | NSNumericSearch | 
     NSWidthInsensitiveSearch | NSForcedOrderingSearch; 

    NSRange string1Range = NSMakeRange(0, [string1 length]); 

    return [string1 compare:string2 
        options:comparisonOptions 
        range:string1Range 
        locale:(NSLocale *)locale]; 
} 

請確保您將函數定義複製到頭中,否則會在您的排序數組中出現編譯錯誤。

爲了您的數組排序,使用此方法:

[mGlossaryArray sortedArrayUsingFunction:finderSortWithLocale context:[NSLocale currentLocale]]; 

你的結果會是這樣的:

  • Ç
  • 網吧
  • 癌症
  • 中國
  • 基督教
  • 聖誕
  • 可樂
5

我可以建議使用-localizedStandardCompare:(NSString的)? 「

」只要文件名或其他字符串在類似Finder的排序適當的列表和表格中顯示,就應該使用此方法。在不同的語言環境下,此方法的確切排序行爲是不同的,並且在將來的版本中可能會更改。 「

2

此代碼適用於我。

- (void)sortSearchResultWithInDocumentTypeArray:(NSMutableArray *)aResultArray basedOn:(NSString *)aSearchString { 

    NSSortDescriptor * frequencyDescriptor =[[NSSortDescriptor alloc] initWithKey:aSearchString ascending:YES comparator:^(id firstDocumentName, id secondDocumentName) { 

     static NSStringCompareOptions comparisonOptions = 
     NSCaseInsensitiveSearch | NSNumericSearch | 
     NSWidthInsensitiveSearch | NSForcedOrderingSearch; 

     return [firstDocumentName compare:secondDocumentName options:comparisonOptions]; 
    }]; 

    NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil]; 
    [aResultArray sortUsingDescriptors:descriptors]; 
} 
3

你可以使用這個根據名字這就是數組排序也包含小寫字母

NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"w" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

NSArray *sortDescriptors = [NSArray arrayWithObject:sorter]; 

[mGlossaryArray sortUsingDescriptors:sortDescriptors]; 

此代碼工作正常,我按照字母排序名稱也已小字符即岩石,阿賈伊,約翰,鮑勃等

38

只需使用NSSortDescriptor就像我用它和它工作得很好。

NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
2

蘋果的取景器排序與區域法的另一種形式使用比較塊,有用的,如果你是在ARC環境,不想應付橋接石膏等:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"your_string_key" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { 
    NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch; 
    NSRange string1Range = NSMakeRange(0, ((NSString *)obj1).length); 
    return [(NSString *)obj1 compare: (NSString *)obj2 options: comparisonOptions range: string1Range locale: [NSLocale currentLocale]]; 
}]; 

NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[sortDescriptor]]; 

爲了提高效率,我也建議將當前語言環境存儲在本地變量中。