Custom Linq Ordering

前端 未结 5 1493
小蘑菇
小蘑菇 2021-02-09 00:34

I have over a thousand folders, each folder contains one or more files with the following names:

Unordered:

Alison.ext
Heather.ext
Molly.ext
Paula.ext
Sam.ext         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 01:05

    Here's a method that produces keys for ordering

    public int OrderKey(string fileName)
    {
      char first = fileName[0];
      int result =
         first  == 'M' ? 1 :
         first  == 'S' ? 2 :
         first  == 'H' ? 3 :
         first  == 'A' ? 4 :
         first  == 'P' ? 5 :
         6;
      return result;
    }
    

    Here's how to call it:

    List ordered = Files.OrderBy(f => OrderKey(f.FileName)).ToList();
    

提交回复
热议问题