C# Sort files by natural number ordering in the name?

后端 未结 7 1120
刺人心
刺人心 2020-12-03 18:09

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:



        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 18:41

    Alphabetically, the "wrong" order is in fact correct. If you want it sorted numerically then you'll need to either:

    1. convert the filenames to a list of numeric numbers and sort them
    2. name the files in such a way that alphabetic and numeric sorting are the same (0-001.jpeg and 0-030.jpg)
    3. 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.

提交回复
热议问题