sorting a list of Complex type in C#

Sorting a List of Complex Type in C#

In this article, we will discuss sorting a list of Complex type like Employee, Customer, Product, Department, etc. with an example. Please read our last article before proceeding to this article. In our last article, we discuss how to sort a list of the simple type like int, double, decimal, string, etc. with an example.

In our last article, we discussed that sorting a list of the simple type like int, double, char, string, etc. is straightforward. Means we just need to invoke the Sort() method (Provided by the Generic List class) on the list instance and the data will be automatically sorted in ascending order.

For Example, if we have a list of integers as shown below

List<int> numbersList = new List<int>{ 1, 8, 7, 5, 2, 3, 4, 9, 6 };

Then we need to invoke the Sort() method on numbersList collection as shown below

numbersList.Sort();

However, when we do the same thing on a complex type like Employee, Product, Customer, Department, etc. we get a runtime exception as “invalid operation exception – Failed to compare 2 elements in the array”. 

Let’s see this practically.
When we execute the above code, it will give us the below runtime exception.

 

sorting a list of Complex type in C#

To sort a list of complex type without using LINQ, the complex type has to implement the IComparable interface and need to provide the implementation for the CompareTo() method. The CompareTo() method returns an integer value and the meaning of the return value as shown below.

A return value greater than ZERO – The current instance is greater than the object being compared with.

Return value less than ZERO – The current instance is less than the object being compared with.

The return value is ZERO – The current instance is equal to the object being compared with.

Alternatively, we can also invoke CompareTo() method directly. The Salary property of the Employee object is int and the CompareTo() method is already implemented on integer type that we already discussed in our previous article, so we can invoke this method and return it’s value as shown below.

return this.Salary.CompareTo(obj.Salary);

Let see an example for better understanding.

Modify the Employee class as shown below.

Now run the application. It will give you the result in ascending order based on the Employee Salary as shown in the below image.

sorting a list of Complex type in C#

If you prefer not to use the Sort functionality provided by the Employee class, then you can provide our own implementation by implementing the IComparer interface.

For example, if you want the employees to be sorted by the name instead of salary. Then need to follow below two steps.

Step1: Implement IComparer interface

Step2: Pass an instance of the class that implements IComparer interface, as an argument to the Sort() method.

SortByName sortByName = new SortByName();

listEmployees.Sort(sortByName);

Here is the complete code:

OUTPUT:

sorting a list of Complex type in C#

In the next article, I will discuss how to sort a list of complex type using comparison delegate in C# with an example.

SUMMARY:

In this article, I try to explain sorting a list of Complex type in C# with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *