排序不正确 [英] Sorting is not working correctly
问题描述
我使用自定义命名通过"app1.format","file2.format"等通过我的应用程序将一些文件保存在文档目录中.稍后,我将这些文件提取到Array中并循环打印,然后将它们以排序形式出现,但当我存储"file10.format"等时出现问题.在此之后,结果是出乎意料的.保存第10个文件后,输出就像
I am saving some files in document directory through my app using custom naming as "file1.format" ,"file2.format" and so on.Later I fetch these files in an Array and printing them in a loop then they are coming in sorted form but the problem arises when I store "file10.format" and so on. After this the result comes is some what unexpected. As after saving 10th file the output comes like
file1.foramtfile10.formatfile2.format..file6.formatfile61.formatfile7.format我不知道为什么排序将所有1或2s放在一个地方,如上所示,而预计10应该排在9之后而不是1之后.我使用了各种排序方法,但是结果总是相同.>
file1.foramt file10.format file2.format . . file6.format file61.format file7.format I don't know why sorting take all 1s or 2s on one place as shown above while it is expected that 10 should just comes after 9 not after 1.I used all kind of sorting but the result is coming same all time.
推荐答案
如果要像Finder一样"对文件名进行排序,请使用 localizedStandardCompare
.特别是,字符串中的数字是根据其数字值排序的:
If you want to sort file names "as the Finder does", use localizedStandardCompare
. In particular, numbers in the strings are sorted according to their numeric value:
NSArray *files = [NSArray arrayWithObjects:@"file10.format", @"file2.format", @"file1.format", nil];
NSArray *sorted = [files sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
NSLog(@"%@", sorted);
输出:
2012-11-05 11:38:55.474 test77[533:403] (
"file1.format",
"file2.format",
"file10.format"
)
这篇关于排序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!