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 of complex types using Comparison delegate in C#?
Sorting a list of complex types in C# can be achieved using the Comparison delegate with the Sort() method. The List<T>.Sort() method has an overload that accepts a Comparison<T> delegate, allowing you to define custom sorting logic for complex objects.
The Comparison<T> delegate represents a method that compares two objects of the same type and returns an integer indicating their relative order.
Syntax
Following is the syntax for the Sort() method using a Comparison<T> delegate −
public void Sort(Comparison<T> comparison)
The comparison delegate signature is −
public delegate int Comparison<in T>(T x, T y)
Return Value
The comparison method returns an integer that indicates the relative order of the objects being compared −
-
Negative value: First object is less than the second object
-
Zero: Both objects are equal
-
Positive value: First object is greater than the second object
Using Lambda Expression for Sorting
The most common approach is to use a lambda expression with the Comparison<T> delegate −
using System;
using System.Collections.Generic;
public class Employee {
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
class Program {
public static void Main() {
Employee Employee1 = new Employee() {
ID = 101,
Name = "Mark",
Salary = 4000
};
Employee Employee2 = new Employee() {
ID = 103,
Name = "John",
Salary = 7000
};
Employee Employee3 = new Employee() {
ID = 102,
Name = "Ken",
Salary = 5500
};
List<Employee> listEmployees = new List<Employee>();
listEmployees.Add(Employee1);
listEmployees.Add(Employee2);
listEmployees.Add(Employee3);
Console.WriteLine("Employees before sorting");
foreach (Employee Employee in listEmployees) {
Console.WriteLine("ID: " + Employee.ID + ", Name: " + Employee.Name);
}
listEmployees.Sort((x, y) => x.ID.CompareTo(y.ID));
Console.WriteLine("\nEmployees after sorting by ID");
foreach (Employee Employee in listEmployees) {
Console.WriteLine("ID: " + Employee.ID + ", Name: " + Employee.Name);
}
listEmployees.Sort((x, y) => y.Salary.CompareTo(x.Salary));
Console.WriteLine("\nEmployees sorted by Salary (descending)");
foreach (Employee Employee in listEmployees) {
Console.WriteLine("ID: " + Employee.ID + ", Salary: " + Employee.Salary);
}
}
}
The output of the above code is −
Employees before sorting ID: 101, Name: Mark ID: 103, Name: John ID: 102, Name: Ken Employees after sorting by ID ID: 101, Name: Mark ID: 102, Name: Ken ID: 103, Name: John Employees sorted by Salary (descending) ID: 103, Salary: 7000 ID: 102, Salary: 5500 ID: 101, Salary: 4000
Using Named Method for Comparison
You can also define a separate method and pass it as a delegate −
using System;
using System.Collections.Generic;
public class Employee {
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
class Program {
private static int CompareByName(Employee x, Employee y) {
return x.Name.CompareTo(y.Name);
}
public static void Main() {
List<Employee> employees = new List<Employee> {
new Employee { ID = 101, Name = "Mark", Salary = 4000 },
new Employee { ID = 103, Name = "John", Salary = 7000 },
new Employee { ID = 102, Name = "Ken", Salary = 5500 }
};
Console.WriteLine("Before sorting by Name:");
foreach (Employee emp in employees) {
Console.WriteLine("Name: " + emp.Name + ", ID: " + emp.ID);
}
employees.Sort(CompareByName);
Console.WriteLine("\nAfter sorting by Name:");
foreach (Employee emp in employees) {
Console.WriteLine("Name: " + emp.Name + ", ID: " + emp.ID);
}
}
}
The output of the above code is −
Before sorting by Name: Name: Mark, ID: 101 Name: John, ID: 103 Name: Ken, ID: 102 After sorting by Name: Name: John, ID: 103 Name: Ken, ID: 102 Name: Mark, ID: 101
Multiple Criteria Sorting
You can implement complex sorting logic with multiple criteria using the Comparison<T> delegate −
using System;
using System.Collections.Generic;
public class Employee {
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
class Program {
public static void Main() {
List<Employee> employees = new List<Employee> {
new Employee { ID = 101, Name = "Mark", Salary = 5000 },
new Employee { ID = 103, Name = "John", Salary = 5000 },
new Employee { ID = 102, Name = "Ken", Salary = 4000 }
};
Console.WriteLine("Before sorting:");
foreach (Employee emp in employees) {
Console.WriteLine($"Name: {emp.Name}, Salary: {emp.Salary}, ID: {emp.ID}");
}
// Sort by Salary first, then by Name
employees.Sort((x, y) => {
int salaryComparison = y.Salary.CompareTo(x.Salary); // Descending by salary
if (salaryComparison == 0) {
return x.Name.CompareTo(y.Name); // Ascending by name if salary is same
}
return salaryComparison;
});
Console.WriteLine("\nAfter sorting by Salary (desc), then Name (asc):");
foreach (Employee emp in employees) {
Console.WriteLine($"Name: {emp.Name}, Salary: {emp.Salary}, ID: {emp.ID}");
}
}
}
The output of the above code is −
Before sorting: Name: Mark, Salary: 5000, ID: 101 Name: John, Salary: 5000, ID: 103 Name: Ken, Salary: 4000, ID: 102 After sorting by Salary (desc), then Name (asc): Name: John, Salary: 5000, ID: 103 Name: Mark, Salary: 5000, ID: 101 Name: Ken, Salary: 4000, ID: 102
Conclusion
The Comparison<T> delegate provides a flexible way to sort lists of complex types in C#. You can use lambda expressions for simple comparisons or define separate methods for complex sorting logic. This approach allows custom sorting based on any property or combination of properties of your objects.
