Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to sort a list in C#?
Sorting a list in C# can be accomplished using several methods. The List<T> class provides the Sort() method for in-place sorting, while LINQ offers additional sorting capabilities. This article demonstrates various approaches to sort lists in ascending and descending order.
Syntax
Following is the syntax for the basic Sort() method −
list.Sort();
Following is the syntax for sorting with a custom comparer −
list.Sort((x, y) => y.CompareTo(x)); // descending order
Using List.Sort() Method
The Sort() method sorts the elements in the entire list using the default comparer for the list element type. This method modifies the original list −
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
var cars = new List<string>() {"Mercedes", "Audi", "Jaguar"};
Console.WriteLine("Original List:");
foreach (var name in cars) {
Console.WriteLine(name);
}
// sort in ascending order
cars.Sort();
Console.WriteLine("\nSorted List (Ascending):");
foreach (var name in cars) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original List: Mercedes Audi Jaguar Sorted List (Ascending): Audi Jaguar Mercedes
Sorting in Descending Order
To sort a list in descending order, you can use a custom comparer or the Reverse() method after sorting −
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
var numbers = new List<int>() {45, 12, 78, 23, 67};
Console.WriteLine("Original List:");
foreach (var num in numbers) {
Console.Write(num + " ");
}
// sort in descending order using custom comparer
numbers.Sort((x, y) => y.CompareTo(x));
Console.WriteLine("<br>\nSorted List (Descending):");
foreach (var num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original List: 45 12 78 23 67 Sorted List (Descending): 78 67 45 23 12
Using LINQ OrderBy() Method
LINQ's OrderBy() method creates a new sorted sequence without modifying the original list. This is useful when you want to preserve the original list −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
public static void Main() {
var fruits = new List<string>() {"Banana", "Apple", "Orange", "Cherry"};
Console.WriteLine("Original List:");
foreach (var fruit in fruits) {
Console.WriteLine(fruit);
}
// create sorted sequence using LINQ
var sortedFruits = fruits.OrderBy(x => x).ToList();
Console.WriteLine("\nSorted using LINQ OrderBy:");
foreach (var fruit in sortedFruits) {
Console.WriteLine(fruit);
}
// original list remains unchanged
Console.WriteLine("\nOriginal list after LINQ sort:");
foreach (var fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original List: Banana Apple Orange Cherry Sorted using LINQ OrderBy: Apple Banana Cherry Orange Original list after LINQ sort: Banana Apple Orange Cherry
Comparison of Sorting Methods
| Method | Modifies Original | Returns | Performance |
|---|---|---|---|
| List.Sort() | Yes | void | Faster (in-place) |
| LINQ OrderBy() | No | New IOrderedEnumerable | Slower (creates new sequence) |
| List.Sort() + Reverse() | Yes | void | Moderate |
Conclusion
C# provides multiple ways to sort lists: List.Sort() for in-place sorting, custom comparers for descending order, and LINQ OrderBy() for creating new sorted sequences. Choose Sort() for performance when modifying the original list is acceptable, or use LINQ when you need to preserve the original data.
