由自然数的名义订购C#排序文件? [英] C# Sort files by natural number ordering in the name?
本文介绍了由自然数的名义订购C#排序文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的目录中的文件
0-0.jpeg
0-1.jpeg
0-5.jpeg
0-9.jpeg
0-10.jpeg
0-12.jpeg
...
当我加载文件:
的FileInfo [] =文件di.GetFiles();
他们在得到错误的顺序(他们应该去像以上):
0-0.jpeg
0-1.jpeg
0-10.jpeg
0-12.jpeg
0-5.jpeg
0-9.jpeg
如何解决?
我试图对它们进行排序,但没办法:
1)的Array.Sort(文件(F1,F2)=> f1.Name.CompareTo(f2.Name));2)的Array.Sort(文件,(X,Y)=> StringComparer.OrdinalIgnoreCase.Compare(x.Name,y.Name));
解决方案
按字母顺序,错误的顺序是正确的事实。如果你需要它有序数值,那么你需要的将是:
- 转换的文件名数字编号的列表并将其分类
- 命名以这样的方式,在字母和数字的排序是相同的(0-001.jpeg和0-030.jpg) 中的文件
- 依赖于文件的创建时间排序(presuming文件被为了创建)。
查看答案排序Directory.GetFiles()为#3的例子。
I have files in directory like that
0-0.jpeg
0-1.jpeg
0-5.jpeg
0-9.jpeg
0-10.jpeg
0-12.jpeg
....
when i loading files:
FileInfo[] files = di.GetFiles();
They getting in wrong order (they should go like above):
0-0.jpeg
0-1.jpeg
0-10.jpeg
0-12.jpeg
0-5.jpeg
0-9.jpeg
How to fix that?
I was trying to sort them but no way:
1) Array.Sort(files, (f1, f2) => f1.Name.CompareTo(f2.Name));
2) Array.Sort(files, (x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name));
解决方案
Alphabetically, the "wrong" order is in fact correct. If you want it sorted numerically then you'll need to either:
- convert the filenames to a list of numeric numbers and sort them
- name the files in such a way that alphabetic and numeric sorting are the same (0-001.jpeg and 0-030.jpg)
- rely on the file creation time to sort (presuming the files were created in order).
See the answer to Sorting Directory.GetFiles() for an example of #3.
这篇关于由自然数的名义订购C#排序文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文