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: 0 (0 votes)

This article have been viewed 3060 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();
        }
    }

    
}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C++ Program to display "Hello, World!"

Problem Write a program in C++ to display "Hello, World!" on the screen. C++ Program to...

C# and Lambda - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...

C# and LINQ - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...