如何按数量对列表进行排序? [英] How to sort list of list by count?
问题描述
在C#中:
List<List<Point>> SectionList = new List<List<Point>>();
SectionList包含点列表,其中每个子列表包含多少个点.
SectionList contains lists of points where each sub list varies in how many points it contains.
我要弄清楚的是如何按子列表的Count降序对SectionList进行排序.
What I'm trying to figure out is how to sort SectionList by the Count of sub lists in descending order.
因此,如果SectionList有3个点列表,则排序后,SectionList [0]将包含所有3个列表中最高的Count值.
So if SectionList had 3 lists of points, after sorting, SectionList[0] would contain the highest Count value of all 3 lists.
谢谢, 神话
推荐答案
这应该有效:
SectionList.Sort((a,b) => a.Count - b.Count);
(a,b) => a.Count - b.Count
是一个比较委托. Sort
方法使用成对的列表进行调用,以进行比较,如果a
短于b
,则委托返回一个负数;如果a
长于b
,则返回一个正数;当这两个列表的长度相同.
The (a,b) => a.Count - b.Count
is a comparison delegate. The Sort
method calls it with pairs of lists to compare, and the delegate that returns a negative number if a
is shorter than b
, a positive number if a
is longer than b
, and zero when the two lists are of the same length.
这篇关于如何按数量对列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!