How to sort DirectoryInfo.GetFiles()

Rahul Gokani picture Rahul Gokani · Mar 15, 2014 · Viewed 8.7k times · Source

I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.

My Files: enter image description here

My issue is when I get the files, are not in the sequence I need them in. The files sequence which I am getting in the FileInfo[] is :

enter image description here

Can any one help me to solve this issue?

Answer

evanmcdonnal picture evanmcdonnal · Mar 15, 2014

The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;

   FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();