Building C# Comparable Objects: IComparable versus IComparer - Sorting Objects
(Page 2 of 4 )
In our first attempt to sort objects, we create an Employee class with name and id. In the Main() method we create an array of five employees. The System.Array class provides a Sort() method that sorts an array of objects. When we try to run the following code we end up with a runtime exception: “At least one object must implement IComparable.” In other words, the error is asking us what we mean by sorting employees.
class Program
{
static void Main( string [] args)
{
Employee[] employees = new Employee[5];
employees[0] = new Employee( "Dan K." , 12456);
employees[1] = new Employee( "Jim L." , 99584);
employees[2] = new Employee( "Tony T." , 51472);
employees[3] = new Employee( "Jessica W." , 32788);
employees[4] = new Employee( "Leda B." , 44110);
//**Runtime exception: at least one object must
//**implement IComparable
Array .Sort(employees);
}
}
class Employee
{
private string name;
public string Name
{
get { return name; }
set { name = value ; }
}
private int id;
public int Id
{
get { return id; }
set { id = value ; }
}
public Employee( string a_name, int an_id)
{
name = a_name;
id = an_id;
}
}
It is clear then that we need to implement the IComparable interface to be able to sort the array of employees. The IComparable interface contains a single method called CompareTo:
public int CompareTo( object obj)
The method accepts an object as a parameter and returns an integer. Since object is the parent of all objects, passing an Employee or any other object will work. The return value could be positive, negative, or zero:
A return value of zero means that the two objects we are comparing are equal to each other.
A negative return value means that the first object comes before the second object.
A positive return value means that the first object comes after the second object.
The first thing we do in the CompareTo method is cast the object parameter into an Employee object. The we compare this object with the newly casted Employee object. In this case we decided to compare them by id. Here is how the code will look:
class Program
{
static void Main( string [] args)
{
Employee[] employees = new Employee[5];
employees[0] = new Employee( "Dan K." , 12456);
employees[1] = new Employee( "Jim L." , 99584);
employees[2] = new Employee( "Tony T." , 51472);
employees[3] = new Employee( "Lee W." , 32788);
employees[4] = new Employee( "Leda B." , 44110);
Array .Sort(employees);
foreach (Employee e in employees)
Console .WriteLine(e);
}
}
class Employee : IComparable
{
private string name;
public string Name
{
get { return name; }
set { name = value ; }
}
private int id;
public int Id
{
get { return id; }
set { id = value ; }
}
public Employee( string a_name, int an_id)
{
name = a_name;
id = an_id;
}
public override string ToString()
{
return string .Format( "Name: {0}ttId: {1}" ,
Name, Id);
}
#region IComparable Members
public int CompareTo( object obj)
{
Employee temp = (Employee)obj;
if ( this .Id > temp.Id)
return 1;
if ( this .Id < temp.Id)
return -1;
else
return 0;
}
#endregion
}
In the Main() method, we sort the employees array in place and we use the foreach loop to print the list of employees. Try to run the code and notice in the output how the objects are sorted by id.
Next: Quick Recap >>
More C# Articles
More By Ayad Boudiab