How to make a Type Sortable in C# using IComparable interface?


This content was proof-read by Grammarly. You can Try Grammary for FREE

Assume that you have a type which would be stored in a List. You would want to use List.Sort method to sort your data in the List.

For example , assume that you have to store objects of type Employee in List<Employee> and sort the List by Age using the Sort method. To do this , we can use he IComparable<T> interface.

How to make a Type Sortable in C# using IComparable interface?

The below code snippet demonstrates the usage of the IComparable interface.

public class Employee : IComparable<Employee>
{
    public string Name { get; set; }
    public string Department { get; set; }
    public int Id { get; set; }
    public int Age { get; set; }

    public Employee(string name, int id, int age, string department)
    {
        this.Age = age;
        this.Name = name;
        this.Department = department;
        this.Id = id;
    }
    // Implement the CompareTo method of IComparable interface
    public int CompareTo(Employee other)
    {
        return this.Age.CompareTo(other.Age);
    }
}

When you sort the list of employee objects by calling the List<Employee>.Sort method , the list is sorted using the IComparable<Employee> interface. We just override the CompareTo method and provide an implementation on how to sort your object.

The complete sourcecode of the Employee class implementing Icomparable interface and the output is as shown below.

using System;
using System.Collections.Generic;

namespace ConsoleApplication5
{
    public class Employee : IComparable<Employee>
    {
        public string Name { get; set; }
        public string Department { get; set; }
        public int Id { get; set; }
        public int Age { get; set; }

        public Employee(string name, int id, int age, string department)
        {
            this.Age = age;
            this.Name = name;
            this.Department = department;
            this.Id = id;
        }
        // Implement the CompareTo method of IComparable interface
        public int CompareTo(Employee other)
        {
            return this.Age.CompareTo(other.Age);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>();
            Employee emp1 = new Employee("senthil kumar",1,30,".NET");
            Employee emp2 = new Employee("Norton Stanley", 2, 31, "Java");
            Employee emp3 = new Employee("Rupam Khaitan", 2, 29, "Big Data");
            employees.Add(emp1);
            employees.Add(emp2);
            employees.Add(emp3);

            foreach(var item in employees)
                Console.WriteLine(item.Name);

            Console.WriteLine("-After Sorting-");

            // Call the Sort method of the Employee object
            employees.Sort();

            foreach(var item in employees)
                Console.WriteLine(item.Name);
            Console.ReadLine();
        }
    }

    
}
%d bloggers like this: