2012-06-20 14 views
22

Ich bin ein NSMutableArray Sortierung wie folgt:NSMutableArray Sortierung - Groß- und Kleinschreibung

NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc] autorelease]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray; 
    sortedArray = [ads_printers_array sortedArrayUsingDescriptors:sortDescriptors]; 

Das Problem ist, dass dieser Fall empfindlich ist, und ich möchte es Groß- und Kleinschreibung machen. Wie kann ich das machen? Ich habe versucht, die Dokumentation zu lesen und fand etwas wie folgt aus:

sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc selector: @selector(caseInsensitiveCompare)] autorelease]; 

aber ich habe keine Ahnung, was ich in den Selektor Argument werden setzen sollte. Vielen Dank!

+1

möglich Duplikat [Ich möchte ein Array mit NSSortDescriptor sortieren] (http://stackoverflow.com/questions/5542762/i- want-to-sort-ein-array-using-nssortdescriptor) –

+0

Auschecken folgenden Beitrag: http://StackOverflow.com/a/5542888/200272 –

+1

Vielen Dank, für alle Interessierten die Selektor-Methode, die ich verwendet habe (localizedCaseInsensitiveCompare :) . Prost! – Kevin

Antwort

39
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:str_key 
    ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray 
    arrayWithObject:sortDescriptor]]; 
+0

Diese Lösung hat bei mir funktioniert –

0

Für Swift, gehen Sie wie folgt vor:

let sortDescriptor = NSSortDescriptor(key: sortDescriptorKey, ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))

Verwandte Themen