Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between IComparable and IComparer Interface in C#
The IComparable and IComparer interfaces in C# are both used for sorting and comparing objects, but they serve different purposes and are implemented in different ways.
The IComparable interface allows an object to compare itself with another object of the same type. The IComparer interface provides a way to compare two objects externally, offering more flexibility for custom sorting logic.
Syntax
Following is the syntax for implementing IComparable −
public class ClassName : IComparable {
public int CompareTo(object obj) {
// comparison logic
}
}
Following is the syntax for implementing IComparer −
public class ComparerClass : IComparer {
public int Compare(object x, object y) {
// comparison logic
}
}
Using IComparable Interface
The IComparable interface defines the CompareTo() method that compares the current instance with another object. It returns an integer indicating the relative order −
Example
using System;
class Vehicle : IComparable {
public string make;
public int year;
public Vehicle(string make, int year) {
this.make = make;
this.year = year;
}
public int CompareTo(object obj) {
Vehicle v = (Vehicle)obj;
return String.Compare(this.make, v.make);
}
public override string ToString() {
return make + " (" + year + ")";
}
}
class Program {
public static void Main() {
Vehicle[] vehicles = {
new Vehicle("Toyota", 2020),
new Vehicle("Honda", 2019),
new Vehicle("Ford", 2021)
};
Array.Sort(vehicles);
Console.WriteLine("Sorted by make:");
foreach (Vehicle v in vehicles) {
Console.WriteLine(v);
}
}
}
The output of the above code is −
Sorted by make: Ford (2021) Honda (2019) Toyota (2020)
Using IComparer Interface
The IComparer interface allows you to define custom comparison logic external to the class. This is useful when you need multiple sorting criteria or when you cannot modify the original class −
Example
using System;
using System.Collections;
class Vehicle {
public string make;
public int year;
public Vehicle(string make, int year) {
this.make = make;
this.year = year;
}
public override string ToString() {
return make + " (" + year + ")";
}
}
class YearComparer : IComparer {
public int Compare(object x, object y) {
Vehicle v1 = (Vehicle)x;
Vehicle v2 = (Vehicle)y;
return v1.year.CompareTo(v2.year);
}
}
class Program {
public static void Main() {
Vehicle[] vehicles = {
new Vehicle("Toyota", 2020),
new Vehicle("Honda", 2019),
new Vehicle("Ford", 2021)
};
Array.Sort(vehicles, new YearComparer());
Console.WriteLine("Sorted by year:");
foreach (Vehicle v in vehicles) {
Console.WriteLine(v);
}
}
}
The output of the above code is −
Sorted by year: Honda (2019) Toyota (2020) Ford (2021)
Multiple Sorting Criteria with IComparer
Example
using System;
using System.Collections;
class Vehicle {
public string make;
public int year;
public Vehicle(string make, int year) {
this.make = make;
this.year = year;
}
public override string ToString() {
return make + " (" + year + ")";
}
}
class MakeYearComparer : IComparer {
public int Compare(object x, object y) {
Vehicle v1 = (Vehicle)x;
Vehicle v2 = (Vehicle)y;
int makeComparison = String.Compare(v1.make, v2.make);
if (makeComparison != 0) {
return makeComparison;
}
return v1.year.CompareTo(v2.year);
}
}
class Program {
public static void Main() {
Vehicle[] vehicles = {
new Vehicle("Toyota", 2020),
new Vehicle("Honda", 2021),
new Vehicle("Toyota", 2019),
new Vehicle("Honda", 2020)
};
Array.Sort(vehicles, new MakeYearComparer());
Console.WriteLine("Sorted by make, then by year:");
foreach (Vehicle v in vehicles) {
Console.WriteLine(v);
}
}
}
The output of the above code is −
Sorted by make, then by year: Honda (2020) Honda (2021) Toyota (2019) Toyota (2020)
Comparison
| IComparable | IComparer |
|---|---|
| Implemented by the class being compared | Implemented by a separate comparer class |
| Provides one default comparison | Allows multiple custom comparison methods |
| Uses CompareTo(object obj) method | Uses Compare(object x, object y) method |
| Object compares itself with another | External class compares two objects |
| Requires modifying the original class | Can be added without modifying original class |
Conclusion
Use IComparable when you need a natural ordering for a class and can modify its definition. Use IComparer when you need multiple sorting options or cannot modify the original class. Both interfaces enable flexible object comparison and sorting in C#.
