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
class Student:IComparable<Student>
{
public string Name { set; get; }
public int Age { set; get; }
public int Percentage { set;
get; }
public int CompareTo(Student other)
{
if (this.Percentage
< other.Percentage)
{
return -1;
}
if (this.Percentage
> other.Percentage)
{
return 1;
}
else
{
return 0;
}
}
public override string ToString()
{
return string.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.
class StudentComparer:IComparer<Student>
{
public int 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.
Let we see the Example
class Program
{
static void Main(string[] args)
{
List<Student>
studentlist = new List<Student>();
studentlist.Add(new Student() { Name="Rajesh",
Age=23,Percentage=88});
studentlist.Add(new Student() { Name="Suresh",
Age=21,Percentage=93});
studentlist.Add(new Student() { Name="MeenakshiSundaram",Age=24,Percentage=88});
studentlist.Add(new Student() { Name="Priyanka",Age=20,Percentage=60});
studentlist.Add(new Student() { Name="GeorgeWas",Age=38,Percentage=76});
studentlist.Add(new Student() { Name="Bro",Age=23,Percentage=77});
studentlist.Add(new Student() { Name="SivaRajan",Age=32,Percentage=76});
studentlist.Add(new Student() { Name="Amala",Age=26,Percentage=64});
studentlist.Add(new Student() { Name="Indhumathy",Age=27,Percentage=86});
studentlist.Add(new Student() { Name="KalaiSelvi",Age=25,Percentage=74});
studentlist.Add(new Student() { Name="Pavi",Age=16,Percentage=70});
studentlist.Add(new Student() { Name="Hanish",Age=14,Percentage=94});
Console.WriteLine("Using
IComparable");
studentlist.Sort();
foreach (Student
stud in studentlist)
{
Console.WriteLine(stud.ToString());
}
Console.WriteLine("Using
IComparer");
studentlist.Sort(new StudentComparer());
foreach (Student
stud in studentlist)
{
Console.WriteLine(stud.ToString());
}
Console.Read();
}
}
class StudentComparer:IComparer<Student>
{
public int Compare(Student x, Student
y)
{
if (x.Age < y.Age)
{
return -1;
}
if (x.Age > y.Age)
{
return 1;
}
else
{
return 0;
}
}
}
class Student:IComparable<Student>
{
public string Name { set; get; }
public int Age { set; get; }
public int Percentage { set;
get; }
public int CompareTo(Student other)
{
if (this.Percentage
< other.Percentage)
{
return -1;
}
if (this.Percentage
> other.Percentage)
{
return 1;
}
else
{
return 0;
}
}
public override string ToString()
{
return string.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.
No comments:
Post a Comment