我有一千多个文件夹,每个文件夹都包含一个或多个具有以下名称的文件:
无序:
Alison.ext
Heather.ext
Molly.ext
Paula.ext
Sam.ext
Ordered:
Molly.ext
Sam.ext
Heather.ext
Alison.ext
Paula.ext
我想编写一个表达式来按如上所述对该列表进行排序。
//Creating a dictionary with the custom order
var order = "MSHAP";
var orderDict = order.Select((c,i)=>new {Letter=c, Order=i})
.ToDictionary(o => o.Letter, o => o.Order);
var list = new List<string>{"A.ext", "H.ext", "M.ext", "P.ext", "S.ext"};
//Ordering by the custom criteria
var result = list.OrderBy(item => orderDict[item[0]]);
您可以使用一个很好的辅助方法来处理边缘情况(不存在的字母、null 等),而不是调用 orderDict[item[0]]。但这就是这个想法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)