IComparable: Under the Hood


Description:

We now calling Array.Sort() on C# Types such as int, char, string will automatically do sorting based on that type. But how do we sort array of Employee objects.To make this happen in C# follow the steps below: 

Step#1:

To sort an array of UDC(User Defined Class) objects. First inherit the interface IComparable and implement the method CompareTo.

Step#2:

In the Compare compare the fields the required for sorting and return -1,1,0 based the values if current object field value is greater than passed object field value then return 1 if both are equal then return 0 else return -1 

Step#3:

After done with two steps the Array.Sort() of our UDC objects is will work without any problem. 

Let's run through a coding sample which will make things clear: The source code has a classe called Employee which implements the Interface IComparable since this class is inheriting IComparable interface We have to code for CompareTo() Method which is done.

Source Code Explanation:

1. In the entry point of the Program i.e Main() the program starts with declaring array of employee objects of length 5.

2. Next each object is created using new operator and passing some values To constructor.

3. Next the call the Array.Sort(employees) this is were the IComparable Interface comes to use.When the call is encountered by C# it first looks whether the class implements IComparable interface if it so it looks for CompareTo() method if it is not there then it will cry.Since we have written this method what it will do it will perform quicksort for the array employee by comparing the field specified by us in CompareTo() method and perform necessary swapping of objects not swapping of values to sort the array. ie  when the second object is lesser than first object it swaps first object with second object rather than values of first object against second object.Because of this the Big O of sorting is always O(nlogn)  

4. Hot Tip: What we have if we want to sort the array in descending order rather than ascending simply reverse the return value of 1,-1 in the CompareTo() function.ie if the value of current object is greater then return -1 if it is lesser return 1

5. Now a question arises how to sort the array based on the User-Defined field the solution will be in the Next Article. 

Full Source Code:

// Source Code starts
using System;
class Employee:IComparable
{
private int Id;
private string Name;
public Employee(int id,string name)
{
this.Id=id;
this.Name=name;
}
public int CompareTo(object obj)
{
Employee temp=(Employee)obj;
if(this.Id>temp.Id)
{
return 1;
}
else
{
if(temp.Id==this.Id)
return 0;
else
return -1;
}
}
public static void Main()
{
Employee[] employees=
new Employee[5];
Console.WriteLine("Before Sort:");
for(int i=0;i<employees.Length;i++)
{
employees[i]=
new Employee(5-i,"Employee#" + i);
Console.Write(employees[i].Id + ",");
}
Console.WriteLine();
Array.Sort(employees);
Console.WriteLine("After Sort:");
for(int i=0;i<employees.Length;i++)
{
Console.Write(employees[i].Id + ",");
}
Console.WriteLine();
}
}
// Source Code End

Up Next
    Ebook Download
    View all
    Learn
    View all