C#列表排序的两列 [英] C# list sort by two columns

查看:264
本文介绍了C#列表排序的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要通过两个不同的变量进行排序的是一个布尔值,另一个是一个字符串C#自定义对象名单。我可以排序的或者的的标准,但我无法搞清楚如何把它们结合起来。排序应该是所有的布尔值第一(CheckedIn),然后对每个值的姓氏。现在,我用



  result.Sort((X,Y)=>的String.Compare(x.CheckedIn.ToString( ),y.CheckedIn.ToString())); 
result.Sort((X,Y)=>的String.Compare(x.LastName,y.LastName));



但我怎么能结合则使我的结果是像

  CheckedIn-名称
无 - 艾姆斯
无 - 史密斯
是 - 巴恩斯
是 - 彼得斯


解决方案

  result.Sort((X ,Y)=> x.CheckedIn == y.CheckedIn 
的String.Compare(x.LastName,y.LastName):
(x.CheckedIn -1:1));


I have a C# custom object list that I need to sort by two different variables one is a boolean and the other is a string. I can sort by either of the criteria, but I'm having trouble figuring out how to combine them. The sort should be all of the boolean values first (CheckedIn) and then the last name for each of the values. Right now I use

result.Sort((x, y) => string.Compare(x.CheckedIn.ToString(), y.CheckedIn.ToString()));
result.Sort((x, y) => string.Compare(x.LastName, y.LastName));

But how can I combine then so that my results are like

CheckedIn-Name
No - Aames
No - Smith
Yes - Barnes
Yes - Peters

解决方案

result.Sort((x,y) => x.CheckedIn==y.CheckedIn ? 
  string.Compare(x.LastName, y.LastName) : 
  (x.CheckedIn ? -1 : 1) );

这篇关于C#列表排序的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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