Search code examples
c#icomparable

iComparable Interface Workings


I have created a Customer class with iComparable Interface.

public class Customer: IComparable<Customer>
{
    public int ID {set; get;}
    public string Name {set; get;}
    public int Salary {set; get;}

    public int CompareTo(Customer obj)
    {
      if (this.Salary > obj.Salary)
          return 1;
        else if
            (this.Salary < obj.Salary)
            return -1;
        else
            return 0;
     }

}

In the main program I have created a List of customers and added three customers.

List<Customer> ListCust = new List<Customer>(); 
ListCust.Add(Cust1);
ListCust.Add(Cust2);
ListCust.Add(Cust3);

I then sort the list.

ListCust.Sort();

My question is how is this sort method of list picking up the "CompareTo" part of the Customer class. Initially when I had tried to sort it without the iComparable interface it was giving me invalid operation error.

The following question is why cant I just make a "CompareTo" method in the customer class without iComparable interface and make it work?


Solution

  • The reason .NET could not "just" sort your Customer objects is because it has no way of guessing in what way you want to sort them: by salary, by first name, by last name, by the time they placed their first order, etc.

    However, you can make it work without implementing IComparable in three different ways:

    • Pass a separate IComparer<Customer> implementation - this lets you move comparison logic to a separate class, and apply different comparison logics based on a situation.
    • Pass a Comparison<Customer> delegate - same as above, but now you don't need a separate class; this lets you provide comparison logic in a lambda.
    • Use LINQ's OrderBy instead - Similar to above, but gives you additional capabilities (filtering, projecting, grouping, etc.)