Reputation: 103
I am trying to sort the lists inside a list by their minimum integer value. This is where I am at so far.
var SortedList = from lists in List
orderby lists.List.Min(Min=>Min.value)
ascending
select list ;
For example:
var x = new List<int>{ 5, 10, 4, 3, 0 };
var y = new List<int> { 4, -1, -5, 3, 2 };
var z = new List<int> { 3, 1, 0, -2, 2 };
var ListofLists = new List<List<int>> {x, y, z};
The output of my Linq shall be, to sort the lists by the minimum value in the list of lists. The sequence for the Lists in my example would be:
y -> z -> x
I have tried a lot of linq expressions and searched through the web. The question seems to be very simple... Thanks for any kind of help!
Upvotes: 10
Views: 2133
Reputation: 40526
It's as easy as:
var sorted = ListofLists.OrderBy(l => l.Min());
Or, if you prefer query syntax, you should do:
var sorted = from list in ListofLists
orderby list.Min()
select list;
Here it is in action: https://dotnetfiddle.net/bnfGOG
Upvotes: 13