.NET Framework Class Library provides several interfaces to support additional functionality in collections such as ICollection, IList, and IComparable.
Implementing IComparable
IComparable interface is used to support the sorting of collections. It has one method - CompareTo. When you are designing sortable objects, you must implement the CompareTo method of IComparable.
For example, I create a Developer class, which is inherited from IComparable.
using System;
public class Developer: IComparable
{
public string Name { get; set; }
public int ExperienceInYears { get; set; }
// Constructor
public Developer(string name, int experienceInYears)
{
Name = name;
ExperienceInYears = experienceInYears;
}
// Implement the CompareTo method from IComparable
public int CompareTo(object obj)
{
if (obj == null) return 1; // Null is considered greater
if (obj is Developer otherDeveloper)
{
// Compare based on experience (you can define your own comparison logic here)
return ExperienceInYears.CompareTo(otherDeveloper.ExperienceInYears);
}
throw new ArgumentException("Object is not a Developer");
}
}
Now I implement the CompareTo method like the following.
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
Now let's say the Developer class has a property called ID, and you want your items to be sorted on ID; we simply add the following code.
public int CompareTo(object obj)
{
if (!(obj is Developer))
{
throw new InvalidCastException("Not a valid Developer object.");
}
Developer developer = (Developer)obj;
return this.ID.CompareTo(developer.ID);
}
As you can see from the above code, it basically compares the current value of the object with the new value.
Now let's say the Developer object has public properties called - FirstName, LastName, Age, and Experience, and you want the sorting can be done based on any of these properties.
To do so, I defined an enumeration.
public enum SortFilter
{
FirstName,
LastName,
Age,
Experience
}
After that, I add a static property to the Developer class of type SortFilter.
private static SortFilter sortingBy = SortFilter.FirstName;
public static SortFilter SortingBy
{
get { return sortingBy; }
set { sortingBy = value; }
}
Now I change the CompareTo method to the following.
public int CompareTo(object obj)
{
if (!(obj is Developer))
{
throw new InvalidCastException("Not a valid Developer object.");
}
Developer developer = (Developer)obj;
switch (Developer.SortingBy)
{
case SortFilter.FirstName:
return this.FirstName.CompareTo(developer.FirstName);
case SortFilter.LastName:
return this.LastName.CompareTo(developer.LastName);
case SortFilter.Age:
return this.Age.CompareTo(developer.Age);
case SortFilter.Experience:
return this.Experience.CompareTo(developer.Experience);
default:
goto case SortFilter.FirstName;
}
}

Saravanan GanesanPosted Aug 16, 2023, 6:30 AM
Nice informative article Mahesh !!!!!
Rushi MehtaPosted Sep 11, 2018, 6:20 AM
Nice Article
sunileditedPosted Nov 30, 2005, 7:21 AMEdited May 15, 2006, 2:19 AM
Hi mahesh, We want to sort model collections ( custom data transfer objects) instead of ArrayLists. Since this not a C#.NET collection but a custom collection it doesn't have Sort() method. So how do we go about sorting such a model collection ?