You could use this to keep a "numerical" order
FileInfo[] files = d.GetFiles("*.txt")
.OrderBy(m => m.Name.PadLeft(200, '0')).ToArray();
200 is quite arbitrary, of course.
This will add "as many 0 as needed" so that the file name + n 0 are a string with a length of 200.
to make it a little bit less arbitrary and "brittle", you could do
var f = d.GetFiles("*.txt");
var maxLength = f.Select(l => l.Name.Length).Max() + 1;
FileInfo[] files = f.OrderBy(m => m.Name.PadLeft(maxLength, '0')).ToArray();
How does it work (bad explanation, someone could do better) and why is it far from perfect :
Ordering of characters in c# puts numeric characters before alpha characters.
So 0
(or 9
) will be before a
.
But 10a
, will be before 2a
as 1
comes before 2
.
see char after char
1
/ 2
=> 10a
first
But if we change
10a
and 2a
, with PadLeft
, to
010a
and 002a
, we see, character after character
0
/ 0
=> equal
1
/ 0
=> 002a
first
This "works" in your case, but really depends on your "file naming" logic.
CAUTION : This solution won't work with other file naming logic
For example, the numeric part is not at the start.
f-2-a
and f-10-a
Because
00-f-2-a
would still be before 0-f-10-a
or the "non-numeric part" is not of the same length.
1-abc
and 2-z
Because
01-abc
will come after 0002-z