0

Here's what I have tried:

var vv = new DirectoryInfo(@"C:\Image").GetFileSystemInfos("*.bmp").OrderBy(fs=>int.Parse(fs.Name.Split('_')[1].Substring(0, fs.Name.Split('_')[1].Length - fs.Extension.Length)));

1 Answers1

0

I'd recommend that you create a custom sorter class that implements the IComparer interface to sort the FileSystemInfo array.

Yeah, it's not as sexy as using LINQ, but it is clean

        DirectoryInfo di = new DirectoryInfo("C:\\Image");
        FileSystemInfo[] fi = di.GetFileSystemInfos("*.bmp");

        Array.Sort(fi, new SortByNum());

public class SortByNum : IComparer<FileSystemInfo>
{
    public int Compare( FileSystemInfo x, FileSystemInfo y)
    {
        // extract the numeric portion of the file name
        int val_x = int.Parse(x.Name.Split('_')[1].Substring(0, x.Name.Split('_')[1].Length - x.Extension.Length));
        int val_y = int.Parse(y.Name.Split('_')[1].Substring(0, y.Name.Split('_')[1].Length - y.Extension.Length));

        return val_x.CompareTo(val_y);
    }
}
egray
  • 390
  • 1
  • 4