C# Generics: Collection Interfaces
Pages: 1, 2, 3, 4, 5, 6, 7
Implementing IComparable
Like all collections, the
List implements the Sort( )
method, which allows you to sort any objects that implement
IComparable. In the next example,
you'll modify the Employee object
to implement IComparable:
public class Employee : IComparable<Employee>
To implement the IComparable<Employee>
interface, the Employee object must provide a
CompareTo() method:
public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(r.empID);
}
The CompareTo( ) method takes an
Employee as a parameter. We know this is an
Employee because this is a type-safe collection.
The current Employee object must compare itself to
the Employee passed in as a parameter and return
-1 if it is smaller than the parameter,
1 if it is greater than the parameter, and
0 if it is equal to the parameter. It is up to
Employee to determine what
smallerthan,
greaterthan, and
equalto mean. In this example,
you delegate the comparison to the empId member.
The empId member is an int and
uses the default CompareTo( ) method for integer
types, which will do an integer comparison of the two values.
TIP: The
System.Int32class implementsIComparable<Int32>, so you may delegate the comparison responsibility to integers.
You are now ready to sort
the array list of employees, empList. To see if
the sort is working, you'll need to add integers and
Employee instances to their respective arrays with
random values. To create the random values, you'll
instantiate an object of class Random; to generate
the random values, you'll call the
Next( ) method on the Random
object, which returns a pseudorandom number. The
Next( ) method is overloaded; one version allows
you to pass in an integer that represents the largest random number
you want. In this case, you'll pass in the value
10 to generate a random number between
0 and 10:
Random r = new Random();
r.Next(10);
Example 9-14 creates an integer array and an
Employee array, populates them both with random
numbers, and prints their values. It then sorts both arrays and
prints the new values.
Example 9-14. Sorting an integer and an employee array
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace IComparable
{
// a simple class to store in the array
public class Employee : IComparable<Employee>
{
private int empID;
public Employee( int empID )
{
this.empID = empID;
}
public override string ToString( )
{
return empID.ToString( );
}
public bool Equals( Employee other )
{
if ( this.empID == other.empID )
{
return true;
}
else
{
return false;
}
}
// Comparer delegates back to Employee
// Employee uses the integer's default
// CompareTo method
public int CompareTo( Employee rhs )
{
return this.empID.CompareTo( rhs.empID );
}
}
public class Tester
{
static void Main( )
{
List<Employee> empArray = new List<Employee>( );
List<Int32> intArray = new List<Int32>( );
// generate random numbers for
// both the integers and the
// employee id's
Random r = new Random( );
// populate the array
for ( int i = 0; i < 5; i++ )
{
// add a random employee id
empArray.Add( new Employee( r.Next( 10 ) + 100 ) );
// add a random integer
intArray.Add( r.Next( 10 ) );
}
// display all the contents of the int array
for ( int i = 0; i < intArray.Count; i++ )
{
Console.Write( "{0} ", intArray[i].ToString( ) );
}
Console.WriteLine( "\n" );
// display all the contents of the Employee array
for ( int i = 0; i < empArray.Count; i++ )
{
Console.Write( "{0} ", empArray[i].ToString( ) );
}
Console.WriteLine( "\n" );
// sort and display the int array
intArray.Sort( );
for ( int i = 0; i < intArray.Count; i++ )
{
Console.Write( "{0} ", intArray[i].ToString( ) );
}
Console.WriteLine( "\n" );
// sort and display the employee array
Employee.EmployeeComparer c = Employee.GetComparer( );
empArray.Sort(c);
empArray.Sort( );
// display all the contents of the Employee array
for ( int i = 0; i < empArray.Count; i++ )
{
Console.Write( "{0} ", empArray[i].ToString( ) );
}
Console.WriteLine( "\n" );
}
}
}
Output:
4 5 6 5 7
108 100 101 103 103
4 5 5 6 7
100 101 103 103 108
The output shows that the integer array and
Employee array were generated with random numbers.
When sorted, the display shows the values have been ordered
properly.

