在C#中使用LINQ排序列表 [英] Sort list in C# with LINQ

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

问题描述

我要排序在C#中的列表。

I want to sort a list in C#.

想去哪结构性质AVC变为真那么首先告诉他们,然后去AVC为false。 ?是否有任何的方式在C#中的LINQ to做到这一点。

Like where structure property AVC goes to true then show them first then AVC goes to false. Are any way to do this in C# LINQ?

推荐答案

那么,使用LINQ将是这样的最简单的方法:

Well, the simplest way using LINQ would be something like this:

list = list.OrderBy(x => x.AVC ? 0 : 1)
           .ToList();

list = list.OrderByDescending(x => x.AVC)
           .ToList();



我的认为的那个的自然顺序布尔的值是假LT;真正,但第一种形式使得它更清晰IMO,因为大家都知道, 0℃; 1

I believe that the natural ordering of bool values is false < true, but the first form makes it clearer IMO, because everyone knows that 0 < 1.

请注意,这将不是原来的列表自行解决 - 这将创造一个新的列表,并分配基准回列表变量。如果你想在的地方进行排序,你应该使用 列表< T>的.sort 方法。

Note that this won't sort the original list itself - it will create a new list, and assign the reference back to the list variable. If you want to sort in place, you should use the List<T>.Sort method.

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

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