C# Sort List With Lambda, Comparison Method

Use lambda expressions and LINQ to sort elements in a List. Call the List.Sort method.
Sort List. Many seashells, each different, are on the beach. They could be sorted in many ways. Consider a list—we call Sort, and can provide a Comparison lambda expression.ListLambda
Other methods too can sort a List—these include query expressions in the LINQ syntax. We show how to sort List elements on a property with the orderby keyword.SortProperty
First example. We use Sort() on a List to alphabetize its strings. We can also specify a comparison function, or use the LINQ orderby keyword instead.

Note: This program will populate the List with 3 strings, and sort them alphabetically. We can use the same method for integral types.

Important: The Sort method does not copy the List and sort the copy. It modifies the existing list.

C# program that uses Sort using System; using System.Collections.Generic; class Program { static void Main() { List<string> list = new List<string>(); list.Add("tuna"); list.Add("velvetfish"); list.Add("angler"); // Sort fish alphabetically, in ascending order (A - Z) list.Sort(); foreach (string value in list) { Console.WriteLine(value); } } } Output angler tuna velvetfish
Example, LINQ. Here we use the LINQ orderby keyword to sort a List by any property. This makes it simple to sort based on string length, or a property value in any object type.

IEnumerable: LINQ works on IEnumerable collections, which include List. This syntax is confusing at first, but makes sense.


Contextual: The orderby keyword is called a contextual keyword, and in this place it means to order the List elements by their lengths.

Ascending: You can specify "ascending" or "descending", such as with "orderby element.Length ascending".

orderbyascending, descending
C# program that sorts with LINQ using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list = new List<string>(); list.Add("mississippi"); // Longest. list.Add("indus"); list.Add("danube"); list.Add("nile"); // Shortest. var lengths = from element in list orderby element.Length select element; foreach (string value in lengths) { Console.WriteLine(value); } } } Output nile indus danube mississippi
Example, comparison lambda. The Sort method on List includes overloads that receive a Comparison function. We can specify this as a lambda expression.

Tip: We use a Comparison lambda to handle more complex sorting orders. Here we sort on the first digit of a number.

Note: For your program, you will want to modify the right-hand part of the lambda inside the Sort method call.

Also: To sort in reverse order, we can compare the second argument "b" to the first "a" instead of the opposite order.

Names: The names "a" and "b" are not important for the lambda. We can use identifiers like "left" and "right" instead.

C# program that uses Sort, lambda expression using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int>(); numbers.Add(4); numbers.Add(0); numbers.Add(10); numbers.Add(50); numbers.Add(1000); numbers.Add(40); // ... Sort the numbers by their first digit. // We use ToString on each number. // We access the first character of the string and compare that. // This uses a lambda expression. numbers.Sort((a, b) => (a.ToString()[0].CompareTo(b.ToString()[0]))); Console.WriteLine(":::SORTED BY FIRST DIGIT:::"); foreach (var result in numbers) { Console.WriteLine(result); } } } Output :::SORTED BY FIRST DIGIT::: 0 10 1000 4 40 50
Reverse. We can combine Sort() with the Reverse extension method to get a reverse-sorted collection. Sort works with all value types and classes that implement the CompareTo method.Reverse
A summary. We sorted Lists with the Sort method and LINQ query syntax. The Reverse method can be used to specify that the List be ordered in the opposite way.LINQ
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to