C#列表排序的两列

编程入门 行业动态 更新时间:2024-10-27 20:26:42
本文介绍了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#列表排序的两列

本文发布于:2023-10-28 02:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!