Pages

Sunday, 1 September 2013

Usage of IComparable and IComparer in c#

In this article we are going to see the usage of IComparable and IComparer. For this we are going to create a custom Student Class with three properties Name, Age and Percentage and implement the IComparable<Student>, Which is used to specify the custom class is comparable based on the percentage property in ascending order. IComparable

classStudent:IComparable<Student>

{

publicstring Name { set; get; }

publicint Age { set; get; }

publicint Percentage { set;
get; }

publicint CompareTo(Student other)

{

if (this.Percentage
< other.Percentage)

{

return -1;

}

if (this.Percentage
> other.Percentage)

{

return 1;

}

else

{

return 0;

}

}

publicoverridestring ToString()

{

returnstring.Format(" Name :"+this.Name+", Age :"+this.Age+", Percentage:"+this.Percentage);

}

}

So Let we see where it is uses, By this it indicates the Student class is comparable,While sorting the collection of Student class it takes the CompareTo Method to compare the objects present in collection.

then the collection of student is ordered in Ascending order of Percentage.

IComparer

Now we see about the IComparer, What is the usage of IComparer it is also to compare the two objects and return the order based on the condition specified in Compare Method.

classStudentComparer:IComparer<Student>

{

publicint Compare(Student x, Student
y)

{

if (x.Age < y.Age)

{

return -1;

}

if (x.Age > y.Age)

{

return 1;

}

else

{

return 0;

}

}

}

Where it is uses ,By this we can specify the comparer externally by creating a new instance of that comparer and pass as parameter to the Sort Method of the collection.In this comparer we are ordering the collection in Ascending order based on the Age of the Student.

returnstring.Format(" Name :"+this.Name+", Age :"+this.Age+", Percentage:"+this.Percentage);

}

}

Output:

Using IComparable

Name :Priyanka, Age
:20, Percentage:60

Name :Amala, Age :26,
Percentage:64

Name :Pavi, Age :16,
Percentage:70

Name :KalaiSelvi, Age
:25, Percentage:74

Name :SivaRajan, Age
:32, Percentage:76

Name :GeorgeWas, Age
:38, Percentage:76

Name :Bro, Age :23,
Percentage:77

Name :Indhumathy, Age
:27, Percentage:86

Name
:MeenakshiSundaram, Age :24, Percentage:88

Name :Rajesh, Age
:23, Percentage:88

Name :Suresh, Age
:21, Percentage:93

Name :Hanish, Age
:14, Percentage:94

Using IComparer

Name :Hanish, Age :14, Percentage:94

Name :Pavi, Age :16, Percentage:70

Name :Priyanka, Age :20, Percentage:60

Name :Suresh, Age :21, Percentage:93

Name :Rajesh, Age :23, Percentage:88

Name :Bro, Age :23, Percentage:77

Name :MeenakshiSundaram, Age :24, Percentage:88

Name :KalaiSelvi, Age :25, Percentage:74

Name :Amala, Age :26, Percentage:64

Name :Indhumathy, Age :27, Percentage:86

Name :SivaRajan, Age :32, Percentage:76

Name :GeorgeWas, Age :38, Percentage:76

From the output you can see that, Using Sort() method the collection is sorted in ascending order of Percentage. It takes the CompareTo method to compare the data.When Specifying the instance of StudentComparer explicitly it takes the comparer method to sort and sort the collection in ascending order of Age.

I Hope from this article you can learn the usage of two interfaces IComparable and IComparer.