Sort list in descending order in C#
This post will discuss how to sort a list in descending order in C#.
1. Using Enumerable.OrderByDescending() Method
With LINQ, you can use the Enumerable.OrderByDescending() method to sort all the elements of a list in descending order according to a key or a specified comparer. The following code demonstrates how to use the OrderByDescending() method to sort a list of Car objects in descending order based on the name field.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; using System.Collections.Generic; public class Car { public string name; public int year; public Car(string name, int year) { this.name = name; this.year = year; } public override string ToString() { return "[" + name + ", " + year + "]"; } } public class Example { public static void Main() { List<Car> cars = new List<Car>(); cars.Add(new Car("x", 2012)); cars.Add(new Car("y", 2015)); cars.Add(new Car("z", 2020)); cars.Add(new Car("z", 2016)); var sortedList = cars.OrderByDescending(x => x.name).ToList(); Console.WriteLine(String.Join(", ", sortedList)); } } |
Output:
[z, 2020], [z, 2016], [y, 2015], [x, 2012]
Note that this creates a new, sorted copy of the list. To specify the additional sort criteria, you can call ThenBy() and ThenByDescending() method on the results of a call to OrderBy(), OrderByDescending(), ThenBy(), or ThenByDescending().
For example, the following code compares objects against subsequent fields, first by the name field in descending order, then by the year field in ascending order, i.e., for objects having the same name, year determines the ordering.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Linq; using System.Collections.Generic; public class Car { public string name; public int year; public Car(string name, int year) { this.name = name; this.year = year; } public override string ToString() { return "[" + name + ", " + year + "]"; } } public class Example { public static void Main() { List<Car> cars = new List<Car>(); cars.Add(new Car("x", 2012)); cars.Add(new Car("y", 2015)); cars.Add(new Car("z", 2020)); cars.Add(new Car("z", 2016)); var sortedList = cars.OrderByDescending(x => x.name) .ThenBy(x => x.year) // ThenByDescending() .ToList(); Console.WriteLine(String.Join(", ", sortedList)); } } |
Output:
[z, 2016], [z, 2020], [y, 2015], [x, 2012]
2. Using Enumerable.OrderBy() Method
Alternatively, you can use LINQ’s OrderBy() method, which creates a new list with items of the source list sorted according to the specified key. It can be used as follows to sort a list of integers in descending order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, 3, -8, 7, -1 }; list = list.OrderBy(x => -x).ToList(); Console.WriteLine(String.Join(", ", list)); // 7, 5, 3, -1, -8 } } |
3. Using List.Sort() Method
To in-place sort contents of a list, consider using the List.Sort() method. It sorts the elements of a list using the specified comparer. The following example demonstrates its usage. Note that this doesn’t use LINQ.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, 3, -8, 7, -1 }; list.Sort((x, y) => y.CompareTo(x)); Console.WriteLine(String.Join(", ", list)); // 7, 5, 3, -1, -8 } } |
The no-arg overload of the List.Sort() method sorts the values in ascending order. To reverse the sort order, you may invoke the List.Reverse() method on the sorted list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, 3, -8, 7, -1 }; list.Sort(); list.Reverse(); Console.WriteLine(String.Join(", ", list)); // 7, 5, 3, -1, -8 } } |
That’s all about sorting lists in descending order in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)