How to sort the list by quantity?

In C #:

List<List<Point>> SectionList = new List<List<Point>>(); 

The List section contains lists of points at which each additional list depends on the number of points contained in it.

What I'm trying to understand is to sort the List section by counting subscriptions in descending order.

So, if there were 3 point lists in the SectionList, after sorting, the List [0] section will contain the highest Count value for all 3 lists.

Thanks Mythics

+4
source share
3 answers

This should work:

 SectionList.Sort((a,b) => a.Count - b.Count); 

(a,b) => a.Count - b.Count - delegate of comparison. The Sort method calls it using pairs of lists for comparison, and a delegate that returns a negative number if a shorter than b , a positive number if a greater than b , and zero when two lists are the same length.

+10
source
 var sortedList = SectionList.OrderByDescending(l=>l.Count()).ToList(); 
+7
source

You can create a custom mapper.

 public class ListCountComparer : IComparer<IList> { public int Compare(IList x, IList y) { return x.Count.CompareTo(y.Count); } } 

Then you can sort your list as follows:

 SectionList.Sort(new ListCountComparer()); 

Hope this helps :)

+3
source

Source: https://habr.com/ru/post/1432731/


All Articles