2016-07-22 4 views
0

Ich habe einige Dateien in nsdocumentdirectory, wenn ich die Datei, die Erträgen die Datei mit zufälligem position.I geholt diesen folgenden Code verwenden:wie Datei aus Nsdocumentverzeichnis nach ihrem Namen sortieren?

NSString *downloadDirectory = [Utility bookDownloadFolder]; 
NSString *bookFolder = [[_selectedBook.zipFile lastPathComponent] stringByDeletingPathExtension]; 
NSString *bookFolderFinal = [downloadDirectory stringByAppendingPathComponent:bookFolder]; 
NSMutableArray *retval = [NSMutableArray array]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error = nil; 
NSArray *files = [fileManager contentsOfDirectoryAtPath:bookFolderFinal error:&error]; 

und der Ausgang ist wie folgt aus:

Dateien (“ 1.jpg, 1.txt, 10.jpg, 10.txt 11.jpg, 11.txt, 12.jpg, 12.txt, 13.jpg "," 13.txt "," 2.jpg "," 2.txt "," 3.jpg "," 3.txt "," 4.jpg "," 4.txt "," 5.jpg ", "5.txt", "6.jpg", "6.txt" "7.jpg", "7.txt", "__MACOSX"

Aber ich möchte die Ausgabe in aufsteigender Reihenfolge wie: Dateien ("1.jpg", "1.txt", "2.jpg", "2 .txt ",)

Wenn ich localizedCaseInsensitiveCompare verwende, um das Array zu sortieren, funktioniert es in diesem Fall nicht, wenn ich localizedCaseInsensitiveCompare verwende, dann ist die Ausgabe nur so. (" __MACOSX "," 1.jpg "," 1.txt "," 10.jpg "," 10.txt "," 11.jpg "," 11.txt "," 12.jpg "," 12.txt "," 13.jpg "," 13. txt "," 2.jpg "," 2.txt "," 3.jpg "," 3.txt "," 4.jpg "," 4.txt "," 5.jpg ",)

+0

Mögliche Duplikate von [Wie sortiere ich ein NSArray alphabetisch?] (Http://StackOverflow.com/questions/1351182/How-to-sort-a-nsarray-alphabetisch) – Droppy

+0

Kein localizedCaseInsensitiveCompare funktioniert in diesem Fall nicht, wenn ich localizedCaseInsensitiveCompare verwenden dann ist die Ausgabe wie diese nur. ("__MACOSX", "1.jpg", "1.txt", "10.jpg", "10.txt", „11.jpg “ "11.txt" "12.jpg" "12.txt" "13.jpg" "13.txt" "2.jpg" "2.txt" , "3.jpg", "3.txt", "4.jpg", "4.txt", "5.jpg", ) –

+1

Sie wollen [diese one] (http://stackoverflow.com/questions/11075644/sort-nsmutablearray-with-strings-die-enthaltenden-Nummern) dann. Sie müssen jedoch Ihre Meinung klären; willst du es numerisch oder nicht? – Droppy

Antwort

1
[NSSortDescriptor sortDescriptorWithKey:@"self" 
ascending:YEScomparator:^(NSString * string1, NSString * string2){ 
return [string1 compare:string2 options:NSNumericSearch]; 
}]; 
0

Swift Beispiel youre Problem zu lösen:

let testArray = ["1.jpg","1.txt","10.jpg","10.txt","11.jpg","11.txt","12.jpg","12.txt","13.jpg","13.txt","2.jpg","2.txt", "3.jpg","3.txt","4.jpg","4.txt","5.jpg","5.txt","6.jpg","6.txt", "7.jpg","7.txt"] 

let sortedArray = testArray.sort({ x, y in 
    return x.localizedStandardCompare(y) == NSComparisonResult.OrderedAscending 
}) 


print(sortedArray) 
//"["1.jpg", "1.txt", "2.jpg", "2.txt", "3.jpg", "3.txt", "4.jpg", "4.txt", "5.jpg", "5.txt", "6.jpg", "6.txt", "7.jpg", "7.txt", "10.jpg", "10.txt", "11.jpg", "11.txt", "12.jpg", "12.txt", "13.jpg", "13.txt"] 
+1

Danke Oleg das wird hilfreich sein .. –

0

NSMutableArray * myArray = [NSMutableArray arraywithobjects: @ "4" @ "2", @ "7", @ "8", Null; // sorting [myArray sortUsingComparator:^NSComparisonResult (NSString * str1, NSString * str2) {return [str1 vergleichen: str2 options: (NSNumericSearch)]; }]; // Protokollierung von NSLog (@ "% @", myArray);

+0

Warum postest du mehrere Antworten? – Droppy

Verwandte Themen