Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list of lists by minimum value

Tags:

c#

list

linq

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!

like image 391
Marius Boshoff Avatar asked Nov 14 '17 09:11

Marius Boshoff


People also ask

How do you find the minimum of a list of numbers?

How to calculate the minimum of a list of numbers? To find the smallest value (the minimum) among a list of numbers, it is necessary to go through the whole list of numbers and compare their values one after the other. The minimum in the list is the smallest value found when all values have been compared.

How do you find the minimum of a list in Python?

The Python min() function returns the lowest value in a list of items. min() can be used to find the smallest number in a list or first string that would appear in the list if the list were ordered alphabetically.

How to sort a list of lists by the specified index?

The function itemgetter () from the operator module takes an index number as a parameter and returns the element from the data set placed at that index number. Therefore, the function sorted (List_name, key=itemgetter (index_number)) sorts a list of lists by the element positioned at the specified index_number of each inner list.

How to sort a list of lists in Python?

Sorting a list of lists arranges all the inner lists according to the specified index as the key. In this tutorial, we will sort a list of lists in Python based on some indexes. The function sorted () is used to sort a list in Python. By default, it sorts the list in ascending order.

How to sort the list of lists by the sum?

How to sort the list of lists by the sum of elements If we want to sort the list according to the sum of elements of the inner list, we can use the key as ‘sum’. 5. Sorting the list of lists in descending order Let us now sort the list in descending order. We can use the reverse parameter of the sort method.

How to sort data according to the 1st Column in ascending order?

To sort the data according to the 1st columns in ascending order. list1=[['Bajaj Pulsar','220 F',40],['Yamaha','YZF R15 Ver 3.0',45],['TVS Apache','rtr 160',50]] # Using the key parameter of sort method. # if we use the sort method, the sorting take place in the list itself list1.sort() print(list1)


1 Answers

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

like image 81
Cristian Lupascu Avatar answered Sep 20 '22 14:09

Cristian Lupascu