Custom sort logic in OrderBy using LINQ

后端 未结 3 1016
时光取名叫无心
时光取名叫无心 2020-11-29 04:01

What would be the right way to sort a list of strings where I want items starting with an underscore \'_\', to be at the bottom of the list, otherwise everything is alphabet

3条回答
  •  佛祖请我去吃肉
    2020-11-29 04:18

    If you want custom ordering, but don't want to supply a comparer, you can have it - sql style:

    autoList
    .OrderBy(a => a.StartsWith("_") ? 2 : 1 )
    .ThenBy(a => a);
    

提交回复
热议问题