- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort a list of complex types using Comparison delegate in C#?
Overloads of the Sort() method in List class expects Comparison delegate to be passed as an argument.
public void Sort(Comparison<T> comparison)
CompareTo returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance.
The Int16.CompareTo() method in C# is used to compare this instance to a specified object or another Int16 instance
Example
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(Employee.ID); } listEmployees.Sort((x, y) => x.ID.CompareTo(y.ID)); Console.WriteLine("Employees after sorting by ID"); foreach (Employee Employee in listEmployees){ Console.WriteLine(Employee.ID); } listEmployees.Reverse(); Console.WriteLine("Employees in descending order of ID"); foreach (Employee Employee in listEmployees){ Console.WriteLine(Employee.ID); } } // Approach 1 - Step 1 // Method that contains the logic to compare Employees private static int CompareEmployees(Employee c1, Employee c2){ return c1.ID.CompareTo(c2.ID); } } public class Employee{ public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } }
Output
Employees before sorting 101 103 102 Employees after sorting by ID 101 102 103 Employees in descending order of ID 103 102 101
- Related Articles
- How to declare a delegate in C#?
- How to sort a list in C#?
- C program to sort a given list of numbers in ascending order using Bubble sort
- Comparison of double and float primitive types in C#
- How to use LINQ to sort a list in C#?
- Action Delegate in C#
- How to sort a list of dictionaries by values of dictionaries in C#?
- How to sort an HTML list using JavaScript?
- How to sort a list of strings in Python?
- How to sort a list by name using a Comparator interface in Java?
- Python How to sort a list of strings
- What is the difference between Func delegate and Action delegate in C#?
- How to sort List in descending order using Comparator in Java
- Sort List in C++
- Ways to sort list of dictionaries using values in python

Advertisements