Custom Linq Ordering

前端 未结 5 1507
小蘑菇
小蘑菇 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 00:53

    List sortKeys = new List { 'M', 'S', 'H', 'A', 'P' };
    sortKeys.Reverse();
    List files = new List(6);
    
    foreach(char sortKey in sortKeys)
    {
        var topFiles = files.Where(file => file.Name.StartsWith(sortKey.ToString()));
        var remainingFiles = files.Except(topFiles);
        files = topFiles.Concat(remainingFiles).ToList();
    }
    

    Untested and I'm sure there are faster ways, but at least it's with linq stuff as you asked :-)

    edit: I just saw the edit on your post and now I don't have any idea anymore what you really want to do, so my code is probably useless to you..

提交回复
热议问题