Sorting List of Complex Types in C#

This article is related to sorting of lists in C# containing complex objects. Through this article we will understand the use of two different interfaces of C#. If we have a list built of primitive types like integer, string and so on and if we call the sort method over that list then we will get a sorted list. But if our list contains complex objects then if we call a sort method over that list then we will get an error. This is because on calling the sort method the list does not know how to sort a complex object because that complex object may contain various fields or properties.

I have created a class Employee as in the following:

  1. class Employee  
  2. {  
  3.    public int Id { getset; }  
  4.     public string Name { getset; }  

If I create a list of Type Employee and add some objects in that like:

  1. List<Employee> employeeList = new List<Employee>();  
  2. Employee ob = new Employee();  
  3. ob.Id = 2;  
  4. ob.Name = "Ritesh";  
  5. employeeList.Add(ob);  
  6. ob = new Employee();  
  7. ob.Id = 1;  
  8. ob.Name = "Sophi";  
  9. employeeList.Add(ob);  
  10. ob = new Employee();  
  11. ob.Id = 3;  
  12. ob.Name = "Aman";  
  13. employeeList.Add(ob);  
  14. employeeList.Sort(); // error line  
  15. foreach (var item in employeeList)  
  16. {  
  17.     Console.WriteLine(item.Id + " " + item.Name);  

Then on calling the sort method over the list we will get the following error:


Now if we want to sort the list of complex objects then we need to implement the IComparable interface that gives us a CompareTo method. Now our class will look like:
  1. Class Employee : IComparable<Employee>  
  2. {  
  3.     Public int Id { getset; }  
  4.     Public string Name { getset; }  
  5.     Public int CompareTo(Employee other)  
  6.     {  
  7.        return this.Id.CompareTo(other.Id);  
  8.     }  

In this class we have compared the objects using an Id property of the class so on calling the sort method of the list we will get the sorted list based upon Id property.

Now if we want to sort the list based upon the Name property then we must implement something different. We need an IComparer interface that provides a mechanism for additional comparisons. This interface has two versions, a simple one and a generic one. Generic is preferred since it prevents boxing and unboxing of objects. So to implement the IComparer interface we will create another class that implements the IComparer interface of type Employee.

  1. class EmployeeSortByName : IComparer<Employee>  
  2. {  
  3.     public int Compare(Employee x, Employee y)  
  4.     {  
  5.         return x.Name.CompareTo(y.Name);  
  6.     }  

In the preceding class we have a Compare method with the two parameters x and y. The Compare method internally calls the CompareTo method to compare the objects by name.

So now if we want to sort the list then we will modify our Sort method and call it like:

  1. employeeList.Sort(new EmployeeSortByName()); 

And we will get a sorted list based upon name.

Similar Articles