C# IComparable tutorial
C# IComparable tutorial shows how to compare values in C# with IComparable
interface. C# tutorial
is a comprehensive tutorial on C# language.
C# IComparable interface
The IComparable
interface defines a generalized type-specific
comparison method that a value type or class implements to order or sort
its instances.
The IComparable
is implemented by types whose values can be ordered
or sorted. The interface requires the CoompareTo()
method to be
implemented. The implemented method is automatically called by methods such
as Array.Sort()
and ArrayList.Sort()
.
The interface is used by types that the programmer has control over; in other
words by code that he has written. If the code is provided by someone else,
we use the IComparer
interface instead.
C# IComparable example
In the following example, we sort a list of employees.
using System; using System.Collections.Generic; namespace CompareEmployeeEx { class Employee : IComparable<Employee> { public string Name { get; set; } public int Salary { get; set; } public int CompareTo(Employee other) { if (this.Salary < other.Salary) { return 1; } else if (this.Salary > other.Salary) { return -1; } else { return 0; } } public override string ToString() { return $"{this.Name} {this.Salary}"; } } class Program { static void Main(string[] args) { var employees = new List<Employee>(); employees.Add(new Employee() { Name = "John Doe", Salary = 1230 }); employees.Add(new Employee() { Name = "Lucy Novak", Salary = 670 }); employees.Add(new Employee() { Name = "Robin Brown", Salary = 2300 }); employees.Add(new Employee() { Name = "Joe Draker", Salary = 1190 }); employees.Add(new Employee() { Name = "Janet Does", Salary = 980 }); employees.Sort(); employees.ForEach(employee => Console.WriteLine(employee)); } } }
In the example, we provide an implementation of the CompareTo()
method of the Employee
class. The class implements the
IComparable
interface.
public int CompareTo(Employee other) { if (this.Salary < other.Salary) { return 1; } else if (this.Salary > other.Salary) { return -1; } else { return 0; } }
The implementation of the CompareTo()
method will sort
the employees by their salary.
employees.Sort();
We sort the list. The method takes the implemented CompareTo()
method into account when sorting.
$ dotnet run Robin Brown 2300 John Doe 1230 Joe Draker 1190 Janet Does 980 Lucy Novak 670
This is the output.
C# IComparable example II
In the following example, we sort an array of users.
using System; namespace CompareUsersEx { class User : IComparable<User> { public string Name { get; set; } public string Occupation { get; set; } public int CompareTo(User other) { if (this.Occupation == other.Occupation) { return this.Occupation.CompareTo(other.Occupation); } return this.Occupation.CompareTo(other.Occupation); } public override string ToString() { return $"{this.Name}, {this.Occupation.ToString()}"; } } class Program { static void Main(string[] args) { var users = new User[] { new User() { Name = "Robin", Occupation = "bookseller" }, new User() { Name = "John", Occupation = "gardener" }, new User() { Name = "John", Occupation = "writer" }, new User() { Name = "Janet", Occupation = "teacher" }, new User() { Name = "Andrew", Occupation = "driver" }, new User() { Name = "Lucy", Occupation = "accountant" } }; Array.Sort(users); foreach (var user in users) { Console.WriteLine(user); } Console.WriteLine("********************************"); Array.Reverse(users); foreach (var user in users) { Console.WriteLine(user); } } } }
The example sorts an array of users in ascending and descending order.
public int CompareTo(User other) { if (this.Occupation == other.Occupation) { return this.Occupation.CompareTo(other.Occupation); } return this.Occupation.CompareTo(other.Occupation); }
The method sorts by the occupation.
Array.Sort(users);
We sort the users in ascending order.
Array.Reverse(users);
We sort the users in descending order.
$ dotnet run Lucy, accountant Robin, bookseller Andrew, driver John, gardener Janet, teacher John, writer ******************************** John, writer Janet, teacher John, gardener Andrew, driver Robin, bookseller Lucy, accountant
This is the output.
In this tutorial, we have used IComparable
interface to
sort data in C#.
Read C# tutorial or list all C# tutorials.