IComparable Example
This page was last reviewed on Jun 30, 2021.
Dot Net Perls
IComparable. This C# interface allows custom sorting of objects when implemented. When a class implements this interface, we must add the public method CompareTo.
Custom implementation. We implement custom sorting for a C# class with IComparable. We can sort in ascending and descending orders.
Example code. Examine the Employee class. This class implements IComparable for Employee, which means an Employee instance can be compared with other Employee instances.
Also The Employee class provides the CompareTo() method and the ToString method.
Detail The method returns int—this indicates which Employee should be ordered first.
Here We want to sort by the Salary from highest to lowest. And if Salary is equal, we want to sort alphabetically.
Detail Here we create a List of Employee instances. These are sorted first by Salary and then by Name.
using System; using System.Collections.Generic; class Employee : IComparable<Employee> { public int Salary { get; set; } public string Name { get; set; } public int CompareTo(Employee other) { // Alphabetic sort if salary is equal. [A to Z] if (this.Salary == other.Salary) { return this.Name.CompareTo(other.Name); } // Default to salary sort. [High to low] return other.Salary.CompareTo(this.Salary); } public override string ToString() { // String representation. return this.Salary.ToString() + "," + this.Name; } } class Program { static void Main() { List<Employee> list = new List<Employee>(); list.Add(new Employee() { Name = "Steve", Salary = 10000 }); list.Add(new Employee() { Name = "Janet", Salary = 10000 }); list.Add(new Employee() { Name = "Andrew", Salary = 10000 }); list.Add(new Employee() { Name = "Bill", Salary = 500000 }); list.Add(new Employee() { Name = "Lucy", Salary = 8000 }); // Uses IComparable.CompareTo() list.Sort(); // Uses Employee.ToString foreach (var element in list) { Console.WriteLine(element); } } }
500000,Bill 10000,Andrew 10000,Janet 10000,Steve 8000,Lucy
Reverse sorts. What should you do if you want to sort in reverse order in the CompareTo method? You could simply reverse the order of the CompareTo expression.
return other.Salary.CompareTo(this.Salary);
return this.Salary.CompareTo(other.Salary);
Alternatives. There are alternative ways you can sort collections of objects by two properties. The easiest is probably the orderby clause in the query syntax in System.Linq.
Info CompareTo and IComparable are often faster than LINQ. A LINQ statement will return an IEnumerable that you must copy into a new List.
List syntax. You can specify the Comparison delegate in the lambda expression syntax. In this case, you do not need to implement the IComparable interface at all.
Warning This approach may be less robust and exhibit poorer object-orientation. But in many cases, it is acceptable.
Sort List, Lambda
A summary. Through this example program, we implemented the IComparable interface on a class. The List.Sort method uses this interface to sort an object collection.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 30, 2021 (image).
© 2007-2024 Sam Allen.