How to make a Type Sortable on Different Criteria in C# ?

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 4974 times

IComparable interface is a great way to sort your objects by using the sorting routines of the List. There is a another interface called IComparer<T> which allows the objects to be sorted based on different criteria.

For example , if you wanted to sort the Employee objects by Id , you can create a new class called CompareId which implements the IComparer<Employee> interface as shown below.

public class CompareId : IComparer<Employee>
{
    public int Compare(Employee x, Employee y)
    {
        return x.Id.CompareTo(y.Id);
    }
}

When you sort the List , you pass the instance of the CompareId class to the sort method of the List.

CompareId objId = new CompareId();           
employees.Sort(objId);

The complete sourcecode of the example of Icomparer interface demo is below.

using System;
using System.Collections.Generic;

namespace ConsoleApplication5
{
    public class CompareId : IComparer<Employee>
    {
        public int Compare(Employee x, Employee y)
        {
            return x.Id.CompareTo(y.Id);
        }
    }
    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("Vijay",189,45,"Technology");
            Employee emp2 = new Employee("Ajith Kumar", 120, 50, "Design");
            Employee emp3 = new Employee("Surya", 201, 40, "Testing");
            employees.Add(emp1);
            employees.Add(emp2);
            employees.Add(emp3);

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

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

            // Id Comparer - instance creation
            CompareId objId = new CompareId();           
            employees.Sort(objId);

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

Leave a Comment


x

You might be Interested in these Posts

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...

C# Program to find the ASCII Value of a Character

Problem Write a program in Visual C# to find the ASCII value of the input character. How to find t...