“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
C# Programming Series
C# Collection Classes III
Yashavant Kanetkar
The IComparer Interface
The IComparer interface provides methods to compare two objects. Most of the classes implement this interface so that the user can easily compare two objects of the class.
Let us create a program that shows how to implement the IComparer interface. In this program we would write a class emp maintaining the information of employees. Another class mysort would implement the IComparer interface. Here is the program…
using System ;
using System.Collections ;
namespace icomb
{
class emp
{
public string name ;
public int id ;
public float balance ;
public emp ( int i, string n, float b )
{
id = i ;
name = n ;
balance = b ;
}
public new string ToString( )
{
return id + " " + name + " " + balance ;
}
}
public class mysort : IComparer
{
public int Compare ( object a, object b )
{
int i1 = ( ( emp ) a ).id ;
int i2 = ( ( emp ) b ).id ;
string n1 = ( ( emp ) a ).name ;
string n2 = ( ( emp ) b ).name ;
if ( i1 == i2 )
{
return n1.CompareTo ( n2 ) ;
}
if ( i1 < i2 )
return -1 ;
return 1 ;
}
}
public class Class1
{
public static int Main ( string[ ] args )
{
emp[ ] e =
{
new emp ( 2, "Sanjay", 3450 ),
new emp ( 1, "Rahul", 2500 ) ,
new emp ( 10, "Kavita", 10000 ),
new emp ( 9, "Mohit", 25000),
new emp ( 6, "Sapna", 2500),
} ;
mysort s = new mysort( ) ;
Array.Sort ( e, s ) ;
foreach ( emp s1 in e )
Console.WriteLine ( s1.ToString( ) ) ;
return 0 ;
}
}
}
Here, we have firstly instantiated an array e of type emp and then an object of type mysort. In the mysort class we have defined the Compare( ) method of the IComparer interface and written our own sorting logic in it. Next, to sort the array e we have called the Sort( ) method of the Array class. To the Sort( ) method we have to pass the array to be sorted and reference to the IComparer interface. The Sort( ) method calls the Compare( ) method. The Compare( ) method returns 0, 1 or -1 indicating whether the objects being compared are equal or one is smaller than the other. In our case, since the mysort class is derived from IComparer we have passed reference to the mysort's object. Hence, the Compare( ) method of mysort class gets called.
Once the array is sorted we have displayed the array elements. We have overridden the ToString( ) method in the emp class. This is because we wanted to display the sorted elements using the statement s1.ToString( ). Had we not overridden this method in emp class, ToString( ) of the base class would have got called, which we do not want. In the ToString( ) of emp class we have concatenated the values of the data members in a string and returned it.



