Java Dailly Tip: The difference between Comparator and Comparable

Java Dailly Tip: The difference between Comparator and Comparable


I assume that I have a list of objects and I need to sort it by the given field. I quickly wrote a class that implements Comparator and it works. But looking at my code, I have doubts as to whether I should use the Comparable interface instead of creating a completely new class that implements Comparator.

I assume there is only one field in my class to sort objects by. Is what I have described acceptable in practice?

Is the correct approach to implement the Comparable interface first, rather than creating a new class that implements Comparator? But only if an alternative field comparison is required. If this approach is correct, should you always implement the Comparable interface first, and then the comparator class?


First, let's start with a little theory. Comparable can compare a given object to another object, and the class must implement that object. Using Comparable, we can only implement one sort sequence based on the properties of the class instance.

Comparator can compare two different objects. In this case, the kalsa does not compare its intentions, but the instances of other classes. If we need to implement custom sort types, then we should use Comparator.

In short. Comparable is used to compare two instances of the same class, and Comparator is used to compare instances of different classes. Additionally, Comparable is used if the class must follow the natural order of its objects. The String class implies Comparable. When we require a different sort order then we should complete Comparator.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static java.util.Collections.sort;

public class Person implements Comparable {

    private int id;
    private String name;
    private int age;
    private long salary;

    // Many sort sequences can be created with different names.
    public static Comparator nameComparator = new Comparator() {
        public int compare(Person p1, Person p2) {
    public static Comparator idComparator = new Comparator() {
        public int compare(Person p1, Person p2) {
            return Integer.valueOf(;

    public Person() { }
    public Person(int id, String name, int age, long salary){ = id; = name;
        this.age = age;
        this.salary = salary;
    // setters and getters.

    // Only one sort sequence can be created with in the class.
    public int compareTo(Person p) {
        //return Integer.valueOf(;
        //return Character.toString(;
        if ( > {
            return 1;
        }else if( <{
            return -1;
        }else {
            return Character.toString(;


    public static void main(String[] args) {

        Person p1 = new Person(7, "David", 32, 1200);
        Person p2 = new Person(8, "Kathrine", 45, 36000);

        List list = new ArrayList();
        sort(list); // call @compareTo(o1)
        sort(list, Person.nameComparator); // call @compare (o1,o2)
        sort(list, Person.idComparator); // call @compare (o1,o2)

Share this Post