如何按名称从nsdocumentdirectory中对文件进行排序?

时间:2016-07-22 13:16:03

标签: ios objective-c xcode

我在nsdocumentdirectory中有一些文件,当我获取这些文件时,它返回带有随机位置的文件。我正在使用以下代码:

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];

,输出如下:

文件(" 1.JPG""的1.txt"" 10.JPG"" 10.txt"&# 34; 11.JPG"" 11.txt"" 12.JPG"" 12.txt"" 13.jpg&# 34;," 13.txt"," 2.jpg"," 2.txt"," 3.jpg",&#34 ; 3.txt"" 4.JPG"" 4.txt"" 5.JPG"" 5.txt&#34 ;," 6.jpg"," 6.txt"" 7.jpg"," 7.txt"," __ MACOSX& #34; )

但我希望输出按升序排列如下:文件(" 1.jpg"," 1.txt"," 2.jpg",&# 34; 2.txt",)

如果我使用localizedCaseInsensitiveCompare对数组进行排序,那么在这种情况下它不起作用,如果我使用localizedCaseInsensitiveCompare那么输出就是这样的。(" __ MACOSX"," 1.jpg&# 34;," 1.txt"," 10.jpg"," 10.txt"," 11.jpg",&#34 ; 11.txt"," 12.jpg"," 12.txt"," 13.jpg"," 13.txt&#34 ;," 2.jpg"," 2.txt"," 3.jpg"," 3.txt"," 4.jpg"," 4.txt"," 5.jpg",)

4 个答案:

答案 0 :(得分:2)

解决问题的Swift示例:

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 :(得分:1)

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

答案 2 :(得分:1)

为Swift 4扩展和更新Oleg的答案:

contentsOfDirectory(at:)的文档指出返回的网址的顺序是“未定义的”。我不确定很多开发人员是否期望这样,但无论如何......

第一项工作是获取文件URL:

let maybeURLs = try? FileManager.default.contentsOfDirectory(
        at: rootURL,
        includingPropertiesForKeys: [.isDirectoryKey],
        options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])

guard let urls = maybeURLs else { return }

现在我们必须像Finder那样对URL进行排序。

在这种情况下,由于我们知道所有文件都属于同一目录,我们只能使用lastPathComponent进行比较(幸运的是,这是一个字符串。)

您可以扩展比较逻辑以忽略后缀(.txt vs .jpg)等。

let sortedURLs = urls.sorted { a, b in
    return a.lastPathComponent
        .localizedStandardCompare(b.lastPathComponent)
            == ComparisonResult.orderedAscending
}

打印结果:

sortedURLs.forEach { print($0.lastPathComponent) }

1 Foo.txt
10 Bar.txt
100 Baz.txt
200 Boo.txt
210 Moo.txt

答案 3 :(得分:0)

NSMutableArray * myArray = [NSMutableArray arrayWithObjects:@“4”,@“2”,@“7”,@“8”,nil]; //排序[myArray sortUsingComparator:^ NSComparisonResult(NSString * str1,NSString * str2){return [str1 compare:str2 options:(NSNumericSearch)]; }]; //记录NSLog(@“%@”,myArray);