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));
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
I know this might be late, but here is another solution which works perfectly
FileInfo[] files = di.GetFiles().OrderBy(n => Regex.Replace(n.Name, @"\d+", n => n.Value.PadLeft(4, '0')));
Using Regex replace in the OrderBy Clause
:
Regex.Replace(n.Name, @"\d+", n => n.Value.PadLeft(4, '0'))
So what this does, it pads
the numeric values in the file name with a length of 4 chars in each number:
0-0.jpeg -> 0000-0000.jpeg
0-1.jpeg -> 0000-0001.jpeg
0-5.jpeg -> 0000-0005.jpeg
0-9.jpeg -> 0000-0009.jpeg
0-10.jpeg -> 0000-0010.jpeg
0-12.jpeg -> 0000-0012.jpeg
But this only happens in the OrderBy
clause, it does not touch the original file name in any way. The order you will end up with in the array is the "human natural" order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With