Time to get creative.
FileInfo[] files = di.GetFiles().OrderBy(file =>
Regex.Replace(file.Name, @"\d+", match => match.Value.PadLeft(4, '0'))
);
Using Regex replace in the OrderBy Clause
:
Regex.Replace(file.Name, @"\d+", match => match.Value.PadLeft(4, '0'))
So what this does is it pads
each match of numeric values in the file name with a 0
character to a length of 4 characters
:
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
This only happens in the OrderBy
clause, it does not alter the original file name.
You will end up with the order you are looking for.
If your file names have dates in them, it would still work:
pic_20230124_1528.jpg -> pic_00020000000200030000000100020004_0001000500020008.jpg
pic_20230124_1601.jpg -> pic_00020000000200030000000100020004_0001000600000001.jpg
pic_20230305_0951.jpg -> pic_00020000000200030000000300000005_0000000900050001.jpg