Posted on Updated on


IComparable permits custom sorting of articles when executed. At the point when a class actualizes this interface, we should include people in general strategy CompareTo(T). We execute custom sorting for a class with IComparable.

Interface CompareTo

It would be ideal if you take a gander at the Employee class. This
class executes IComparable<Employee>, which implies an Employee occasion can be contrasted and other Employee examples. The Employee class has two case properties: Salary and Name.

The Employee class provides the CompareTo(Employee) method and the ToString method.

using System;

using System.Collections.Generic;

class Employee : IComparable<Employee>


public int Salary { get; set; }

public string Name { get; set; }

public int CompareTo(Employee other)


// Alphabetic sort if salary is equal. [A to Z]

if (this.Salary == other.Salary)


return this.Name.CompareTo(other.Name);


// Default to salary sort. [High to low]

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


public override string ToString()


// String representation.

return this.Salary.ToString() + “,” + this.Name;



class Program


static void Main()


List<Employee> list = new List<Employee>();

list.Add(new Employee() { Name = “Steve”, Salary = 10000


list.Add(new Employee() { Name = “Janet”, Salary = 10000


list.Add(new Employee() { Name = “Andrew”, Salary = 10000


list.Add(new Employee() { Name = “Bill”, Salary = 500000


list.Add(new Employee() { Name = “Lucy”, Salary = 8000 });

// Uses IComparable.CompareTo()


// Uses Employee.ToString

foreach (var element in list)







Take a gander at the CompareTo strategy execution in the Employee class. The system returns int—this shows which Employee ought to be requested first. In CompareTo, we need to sort by the Salary from most elevated to last.


In the event that Salary is equivalent, we need to sort one after another in order. We check for equivalent Salary values, and all things considered sort sequentially. In the Main strategy, we make List of Employee examples. Every Employee has a Name and a Salary indicated. At that point, we conjure the Sort system on List. This utilizations the IComparable<Employee> usage we included.

At the point when this project is run, the Employee occasions are sorted first by Salary and after that by Name Reverse sorts.

What would it be a good idea for you to would on the off chance that you like to sort backward request in the CompareTo system? For instance, on the off chance that you need to arrange the Employee occasions from most minimal to most astounding pay, you could basically switch the request of the Compare To expression.

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

Here are elective ways you can sort accumulations of articles by two properties. The most straightforward is likely the orderby provision in the question linguistic structure in System.Linq.

CompareTo and IComparable are often faster than LINQ and the orderby clause. The IComparable version reorders the elements in the original list, while the orderby clause will return an IEnumerable that you must copy into a new List.


For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s