Sorting user defined collection in c# using IComparable interface
This article shows how we can sort user defined collections in CSharp. In order to perform sort opertion we are going to implement IComparable interface and use the CompareTo method of this interface to perform sorting on the user defined object. We are going to sort list of Employee objects stored in ArrayList.
This article shows how we can sort user defined collections in CSharp. In order to perform sort opertion we are going to implement IComparable interface and use the CompareTo method of this interface to perform sorting on the user defined object.
Step1: Create a console application in c# and then add an Employee class as below and implement the IComparable interface for this class. We must implement CompareTo method of this interface in this "Employee" class.
//Employee class implementing the IComparable interface to support sorting on Employee object
class Employee : IComparable
{
public int employeeId;
public string Name;
public Employee(int id, string eName)
{
this.employeeId = id;
this.Name = eName;
}
///
/// This method is of IComparable interface used to sort Employee objects.
/// It returns 1 if the employeeid of one employee object is greater than other employee object.
/// It returns -1 if the employeeid of one employee object is less than other employee object.
/// It returns 0 if the employeeid of one employee object is equal to other employee object.
///
///
/// obj is the Employee object passed when the sort method is called by the collection object(alEmployee)
///
public int CompareTo(object obj)
{
Employee otherEmployee = (Employee)obj;
if (this.employeeId > otherEmployee.employeeId)
return 1;
else if (this.employeeId == otherEmployee.employeeId)
return 0;
else
return -1;
}
}
Step2: In the Program.cs file write the below code which creates an ArrayList object and adds few Employee objects to this ArrayList. calling sort method of the ArrayList class performs sort operation on the objects stored in the ArrayList. If the Employee object stored in the ArrayList doesnot implement IComparable interface. Sort method of the ArrayList object throws an exception.
class Program
{
static void Main(string[] args)
{
//alEmployee object is used to store list of employee objects.
ArrayList alEmployee = new ArrayList();
alEmployee.Add(new Employee(1, "sakshi"));
alEmployee.Add(new Employee(3, "rohini"));
alEmployee.Add(new Employee(4, "albert"));
alEmployee.Add(new Employee(2, "preeti"));
Console.WriteLine("Before Sorting");
//Display employeeid before sorting
foreach (Employee emp in alEmployee)
{
Console.WriteLine(emp.employeeId);
}
//When sort method is called on ArrayList object(alEmployee). it passes the current and next employee objects of the arraylist
// to the CompareTo method of the Employee class for sorting.
//Caution: if the Employee class doesnot implement the IComparable interface,
//calling sort method of alEmployee object throws an exception at runtime.
alEmployee.Sort();
Console.WriteLine("After Sorting");
//Display employeeid after sorting
foreach (Employee emp in alEmployee)
{
Console.WriteLine(emp.employeeId);
}
}
}