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

Published on April 22, 2016 by abundantcode

Filed under C#

Last modified April 22, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 5 (1 votes)

This article have been viewed 5902 times

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.

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();
        }
    }

    
}

Leave a Comment


x

You might be Interested in these Posts

C Program to convert lower case string to upper case

Problem Statement Write a program in C to convert the given string from lower case to upper case. ...

How to decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...

How to disable editing of items in a combo box in c#?

When working in Win forms using C# and especially when you use the combo box , you would have notice...