Java — Comparators v/s Comparable

JAYRAJ GONDALIYA
4 min readMay 12, 2020

--

That’s my way…

In programming, Sorting data is always a big deal because of very often and various utilization.

Performing a Sorting on any list, collection or group of data is little bit tricky because there are so many methods of sorting available with pros and cons.

For Example,

For Sorting an array of integer, there are many algorithms such as Merge Sort, Bubble Sort, Insertion sort and etc.

But Sorting a group of objects with respect to specific property is different than sorting a list of integer because we can sort integer with their natural property but in case object it is not possible.

Take an Employee class for example which has fields such as Name, Employee ID, Salary, age.

Based on our requirements, we need to sort group of employees by age, ID, Salary or Name, it could be anything, it is not possible to sort by natural order, and in Java, there are built-in methods available for sorting primitive types, wrapper classes and Lists.

For Example:

int[] a = {9, 8, 5, 6};

Arrays.sort(a);

Output: {5, 6, 8, 9}

This is the in-built way of sorting an integer array, which sorts array based on natural ordering of integer which is ascending but what about array of objects such as Array of employee.

What if we need Employee Array sorted with respect to age or salary?

Comparator and Comparable are the answers of this question, which can do sorting of collection of objects, in our case it is collection of employees.

So, another question here is, if Both do exactly same work, then

what is the difference between comparator and comparable?

Let’s Understand them,

There are some fundamental differences between these two:

Comparable v/s Comparator

1. Comparable Interface:

We need to implement Comparable interface into our class.

Employee class with Comparable Implementation

Note: Comparator and Comparable both use Generic types for compile time type checking so, <Employee> is used.

By Default

  1. Wrapper classes and String class in Java implements Comparable class.
  2. Arrays.sort(x) and Collection.sort(x) uses compareTo() method of Comparable class.

We can override this compareTo() method based on our requirement,

compareTo() method by Employee Id

Now in main class we just need to call default sorting method:

Calling Sort default Method on Array of Employee at Main Class

Output:

Note: I have modified toString() method to print able output, according to that first Column is ID, Name, Age and Salary.

Pros: No need to write logic at client side, by just using sort() method All is set and ready to go.

Cons: Only one type of sorting possible. What if we want to sort with Name, age and Salary in same Class?

2. Comparator Interface:

To use Comparator, we don’t need to implement class but, implementation of compare(Object o1, Object o2) method is needed inside Employee class, which takes two argument as Object and returns response.

Define variable of type Comparator and create new comparator and override compare method, which returns values depending on the comparison property.

4 Different Comparator by different fields inside Employee.Class

Now, comparator have two methods compare() and equals(),

  • compare() returns 0, 1, -1 depending on the value equal, greater than, or less than.

If Object is same, returns 0,

If First Object is greater then second Object, returns 1

If First Object is less then second Object, returns -1

  • equal() returns boolean value depending on equal or not.

Use anyone of them based on your use case.

Now in Main method we need to use default sorting method on collection with our required comparator.

Arrays.sort(Employee[] emp, comparatorType);

Here, Comparator type is functions that we created before (Above Image) and that was static so we can call them without object.

Note: For better output, replaced couple of lines of code from bottom to top.

Output:

Note: Column represents (Emp ID, Name, Age, Salary).

This is my way of using these interfaces and there are several other methods we can use to implement these, not necessarily only this.

Example: If we use comparable, which have only one method, we can use Lambda Expression to implement.

Comparator and Comparable is very useful concept…Keep Learning

Thanks…

--

--