C#元组列表中选择多个排序 [英] c# tuple list multiple sort
本文介绍了C#元组列表中选择多个排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个三维的元组:
var CountList = new List<Tuple<int, int, int>>();
需要用数字上升的第一个INT,则数字下降的第二个INT的排序,所以
The sorting required is numerically ascending on the first int, then numerically descending on the second int, so
5, 4, 7
4, 5, 6
5, 2, 3
3, 5, 2
2, 4, 1
2, 6, 4
变为
2, 6 ,4
2, 4, 1
3, 5, 2
4, 5, 6
5, 4, 7
5, 2, 3
是否有指定的List.Sort()进一步搜索功能的一种方式,或者我需要的信息拆分到单独的列表,执行对每个组的降序排序,然后添加单个列表项目?主目录中的所需升序
Is there a way of specifying further search functions for List.Sort(), or do I need to split the information out to separate lists, perform the descending sort on each group, then add the individual list items to a "master list" in the desired ascending order?
推荐答案
您可以使用LINQ:
CountList = CountList
.OrderBy(t => t.Item1)
.ThenByDescending(t => t.Item2)
.ToList();
使用的 List.Sort
:
CountList.Sort(
(t1, t2) =>
{
int res = t1.Item1.CompareTo(t2.Item1);
return res != 0 ? res : t2.Item2.CompareTo(t1.Item2);
});
List.Sort
排序原来的列表,而不是中创建一个新的。
List.Sort
sorts the original list instead of creating a new one.
这篇关于C#元组列表中选择多个排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文