The List.Sort() method is used to sort the elements of a List<T> in place. By default, it arranges elements in ascending order using their default comparer. It also supports custom sorting based on specific conditions.
- It is available in the System.Collections.Generic namespace.
- It can sort numbers, strings, and custom objects.
- It supports custom comparers and comparison logic for flexible sorting.
Example: Sorting a List in Ascending Order
using System;
using System.Collections.Generic;
class Geeks{
static void Main(){
// Create a list of integers
List<int> numbers = new List<int> { 50, 10, 40, 20, 30 };
// Sort the list in ascending order
numbers.Sort();
// Display sorted elements
Console.WriteLine("Sorted List:");
Console.WriteLine(string.Join(" ", numbers));
}
}
Output
Sorted List: 10 20 30 40 50
Syntax
list.Sort();
For custom sorting:
list.Sort(comparison);
- Parameters: The Sort() method has several overloads. It can accept an IComparer<T>, a Comparison<T> delegate, or a range and comparer. The parameterless overload takes no arguments.
- Return Value: The method returns void and modifies the original list.
Explanation: The Sort() method arranges the integers in ascending order and updates the original list.
Examples of List.Sort()
Sorting a List in Descending Order
We can use a comparison delegate with Sort() to arrange elements in descending order.
using System;
using System.Collections.Generic;
class Geeks{
static void Main(){
// Create a list of integers
List<int> numbers = new List<int> { 50, 10, 40, 20, 30 };
// Sort elements in descending order
numbers.Sort((a, b) => b.CompareTo(a));
// Display sorted elements
Console.WriteLine("Descending Order:");
Console.WriteLine(string.Join(" ", numbers));
}
}
Output
Descending Order: 50 40 30 20 10
Explanation: The comparison (a, b) => b.CompareTo(a) reverses the default comparison order, producing a descending list.
Sorting a List of Strings
The Sort() method can also sort a list of strings using the default string comparer.
using System;
using System.Collections.Generic;
class Geeks{
static void Main(){
// Create a list of strings
List<string> names = new List<string>{
"Rahul", "Amit", "Zoya", "Neha"
};
// Sort the names
names.Sort();
// Display sorted names
Console.WriteLine("Sorted Names:");
Console.WriteLine(string.Join(", ", names));
}
}
Output
Sorted Names: Amit, Neha, Rahul, Zoya
Explanation: The method sorts strings using culture-sensitive comparison rules by default, generally producing alphabetical order for these names.
Sorting a List of Custom Objects
We can also sort custom objects by a specific property using a comparison function.
Example: Sorting Students by Marks
using System;
using System.Collections.Generic;
class Student{
public string Name { get; set; }
public int Marks { get; set; }
}
class Geeks{
static void Main(){
// Create a list of students
List<Student> students = new List<Student>{
new Student { Name = "Amit", Marks = 85 },
new Student { Name = "Rahul", Marks = 70 },
new Student { Name = "Neha", Marks = 95 }
};
// Sort students by marks
students.Sort((a, b) => a.Marks.CompareTo(b.Marks));
// Display sorted students
foreach (Student student in students){
Console.WriteLine($"{student.Name}: {student.Marks}");
}
}
}
Output
Rahul: 70 Amit: 85 Neha: 95
Explanation: The lambda expression compares the Marks property of two students, sorting the list from lowest to highest marks.
Important Points
- List.Sort() modifies the original list instead of creating a new one.
- By default, elements are sorted in ascending order according to their default comparer.
- The method uses an introspective sorting algorithm in modern .NET implementations.
- It is not a stable sort, meaning elements considered equal may not retain their original relative order.
- Custom objects require a suitable default comparison implementation or a custom comparer.