Reputation: 91
I have been working on a c# image viewer that will read images from my computer and show them in the program.
//foreach file in path display the filename
foreach (var filename in Directory.GetFiles(<path>))
{
MessageBox.show(filename);
}
//Get image by number
var image = Directory.GetFiles(<path>).elementatordefault(<picnumber>).tostring());
My problem is that even if my images are ranked in order in the folder: 1,2,3,4 .....12,13,14....101,102, my application will show the files in the following order: 1,101,102,12,13,2...
How would I show the images in the correct like they are in the folder of the pc? I can't believe I would need to add each file to an array or list and then preform a sorting algorithm... (I would also need to split the file path and extension)there must be a simpler way of doing this,any help would be much appreciated.
Upvotes: 0
Views: 1907
Reputation: 91
I finally got round too working out how to sort files in a "natural order" perhaps someone will find this code useful as too how I did it.
List<string> mylist = new List<string> { };
foreach (var f in Directory.GetFiles(FilePath1))
{
mylist.Add(f);
}
var result = mylist.OrderBy(x => x.Length);
Upvotes: 3
Reputation: 153
You need to sort and implement your own comparer, if you know the format of the file names. This thread might help you : Sorting mixed numbers and strings
Upvotes: 0