在使用LINQ排序依据自定义排序逻辑 [英] Custom sort logic in OrderBy using LINQ

查看:254
本文介绍了在使用LINQ排序依据自定义排序逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的方式进行排序,我要开始的项目以下划线_,要在列表的底部字符串列表,否则一切都是按字母顺序排列。

现在我做这样的事情,

  autoList.OrderBy(A => a.StartsWith(_)ZZZZZZ+一:一)
 

解决方案

如果您想自定义排序,但不希望提供一个比较器,你可以把它 - SQL风格:

 自动列表
.OrderBy(?一个=> a.StartsWith(_)2:1)
.ThenBy(一个=&胺a);
 

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 alphabetical.

Right now I'm doing something like this,

autoList.OrderBy(a => a.StartsWith("_") ? "ZZZZZZ"+a : a )

解决方案

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);

这篇关于在使用LINQ排序依据自定义排序逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆