Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort List<List<string>> by length in ascending order

Tags:

c#

May I know how to sort List<List<string>> by the length of List<string> in ascending order?

like image 289
william007 Avatar asked Feb 01 '13 03:02

william007


2 Answers

var result = list.OrderBy(x => x.Length)

like image 181
Federico Berasategui Avatar answered Oct 20 '22 15:10

Federico Berasategui


Have a look at the List<T>.Sort method:

listOfListOfStrings.Sort((a, b) => a.Length.CompareTo(b.Length));

Alternatively, you can create an IEnumerable<List<string>> from the List<List<string>> that returns the lists in sorted order when enumerated, but leaves the original list untouched:

IEnumerable<List<string>> result = listOfListOfStrings.OrderBy(x => x.Length);
like image 36
dtb Avatar answered Oct 20 '22 14:10

dtb