This was one quite a unique question I came across (Maybe someone must have). Someone asked me that can we implement both comparable and comparator interface in a single class? I had slight hint that it might be possible. Since I had never tried such a thing before... I examined it myself.. and hence posting this question so that others my find it quickly over here.
-
Related [Java : Comparable vs Comparator](http://stackoverflow.com/questions/4108604/java-comparable-vs-comparator) – Ruchira Gayan Ranaweera Sep 24 '14 at 12:06
-
Nope... its not a duplicate... Read question carefully first!! @RuchiraGayanRanaweera – DarkHorse Sep 24 '14 at 12:08
-
Yes, It is not duplicate but relevant. – Ruchira Gayan Ranaweera Sep 24 '14 at 12:09
-
More likely, use a "default" Comparator to implement Comparable. – Sep 24 '14 at 12:17
-
technical speaking, doable. practical, doesn't make much sense. It makes codes hard to understand. to be honst, I don't see any advantage from this approach. unless, there were certain requirements, such as to sort/compare a set of `Comparator`s. – Kent Sep 24 '14 at 12:20
-
@Kent Can you elaborate the certain requirement portion? I did not understand it completely.. – DarkHorse Sep 24 '14 at 12:23
-
1@DarkHorse you have 5 StudentComparators, you want to sort the 5 Comparators, what would you do? this is just an example, an extreme example. 99% case we won't (read shouldn't) put comparable comparator together. Assume you want to show the 5 comparators to user (via gui, i.e.), let user choose one of them. And you want the 5 things to be sorted in certain order. – Kent Sep 24 '14 at 12:25
-
@DarkHorse : You should precise in your title and question that you implement Comparator and Comparable of the same type. There are actually some cases where a Comparator could also be a Comparable with no direct relation between A and B. – Dici Sep 24 '14 at 12:37
-
@Kent Nice example... :) – DarkHorse Sep 25 '14 at 02:29
-
Firstly the ans is Yes, but why would you fall into such scenarios, you can implement comparable at class level and if u need to sort the values again on some other base, do use comparator explicitly passing the list. – Yogesh Kumar Nov 26 '17 at 06:11
-
It's an interesting question, but an even more interesting question is whether there's any reason to do it other than to see that it can be done? – Alonso del Arte May 30 '20 at 00:55
3 Answers
Technically you can do this, but this will be bad practice. When class implements Comparable
that means that class has natural order. For example, value classes, such as Integer
implement Comparable
, so you can just pass a list of them into Collections.sort
and get the expected result.
On the other hand, Comparator
is basically designed to be used as anonymous classes when you need to create some custom ordering for other classes. Or to provide ordering for the class that doesn't have natural ordering.
There can be situation, when class should have additional orderings in addition to it's natural ordering. The best way to handle such situation is to create static
constant comparators in such class. For example, if you have class Person
with name
and age
, you can have natural order by name and add additional comparator that will order Persons by age.
class Person implements Comparable<Person> {
public static final Comparator<Person> BY_AGE_COMPARATOR = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.age < o2.age ? -1 : (o1.age == o2.age ? 0 : 1);
}
};
private int age;
private String name;
@Override
public int compareTo(Person o) {
return name.compareTo(o.name);
}
}
This approach is better because you don't need instance of the class to access it's comparator. Also you can add as many additional comparators to the class as you want.
To summarize, you when class implements Comparator
it often means that order some entities is the main purpose of such class. Implementing Comparable
means that instances of this class could be sorted using some natural ordering. Having class both implement Comparable
and Comparator
means that you have natural order for orderings. This doesn't make sense.

- 10,692
- 25
- 39
-
maybe a little picky, never use `return int1 -int2;` to impl. a compare(To) method. well person.age may be fine, but we shouldn't do it anyway. – Kent Sep 24 '14 at 12:42
Pros
If most of the time we have a specific sorting method to be used, Comparable interface's compareTo() method can be used. For example, Employee object most of the times needs to be sorted using the name attribute you will use a compareTo() method of Comparable interface and sort it using employee name. User of the employee object can directly use Collections.sort(empList).
In case when we have a requirement for a specific sorting order, the comparator implementation can be used. So in employee case if we have a requirement to sort by address, we can have an implementation for address and use it as a parameter to Collections.sort(empList, new Employee()); The employee object here would implement compare(Object emp,Object emp1).
Cons
Comparator Implementation should implement the comparison between two objects. Hence we would have to pass the employee object as a comparator which is not very intutive.
Design-wise ideally the comparator method does not belong to employee class as it is not using any attribute of employee object (this) which is calling it.
Yes we can extend any pojo class to implement both comparable and comparator interface and implement the methods defined by them.
I implemented it in sample Student
POJO class.
When called Collections.sort(studentList)
... compareTo() is used.
When called Collections.sort(studentList, new Student())
... compare() is used.
Advantages:
1) I have single class implementation for both comparable and comparator interfaces.
Disadvantages:
1) I can define only one comparable and only one comparator inside one class.
2) Now to define any new comparator I have to move out to other class or define it on the fly (annonymous class).
3) But the motive of comparator is lost.Comparator provides an external implementation for comparison of class objects outside the class. Defining it inside might include confusion as to what is called and when?
People can add more points if I have missed out on any...

- 2,740
- 19
- 28
-
IMO, this answer is part of your question, should be moved into your question? – Kent Sep 24 '14 at 12:10
-
Yes I could have done that... but I wanted it to be in question-answer type... :) – DarkHorse Sep 24 '14 at 12:12