How to sort the next list

I have next var result = new List<Tuple<int, List<ProductionReportEntry>, int>>();

How can I sort it by the last integer that the result will be high to low.

Thanks for the help.

+3


source to share


1 answer


The easiest way is to use LINQ extension method OrderByDescending()

, it will sort your list in descending order from high to low and insert it back into the list:

result = result.OrderByDescending(t => t.Item3).ToList();

      

Suppose you want to save it back to the original link, of course you can assign it to another variable, etc.



Alternatively, you can do an in-place sort using an overload List.Sort()

that the delegate takes Comparisson<T>

:

// does descending since we put r on the lhs and l on the rhs...
result.Sort((l, r) => r.Item3.CompareTo(l.Item3));

      

Alternatively, you can create a custom one IComparer<T>

, of course, for your own Tuple<int, List<PropertyReportEntry>, int>

, but that looks pretty ugly ...

+8


source







All Articles