Java Dailly Tip: The difference between Comparator and Comparable

Java Dailly Tip: The difference between Comparator and Comparable

Problem

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?

Solution

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() {
        @Override
        public int compare(Person p1, Person p2) {
            return p1.name.compareTo(p2.name);
        }
    };
    public static Comparator idComparator = new Comparator() {
        @Override
        public int compare(Person p1, Person p2) {
            return Integer.valueOf(p1.id).compareTo(Integer.valueOf(p2.id));
        }
    };

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

    // Only one sort sequence can be created with in the class.
    @Override
    public int compareTo(Person p) {
        //return Integer.valueOf(this.id).compareTo(Integer.valueOf(e.id));
        //return Character.toString(this.name.charAt(0)).compareToIgnoreCase(Character.toString(e.name.charAt(0)));
        if (this.id > p.id) {
            return 1;
        }else if(this.id < p.id){
            return -1;
        }else {
            return Character.toString(this.name.charAt(0)).compareToIgnoreCase(Character.toString(p.name.charAt(0)));
        }

    }

    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();
        list.add(p1);
        list.add(p2);
        sort(list); // call @compareTo(o1)
        sort(list, Person.nameComparator); // call @compare (o1,o2)
        sort(list, Person.idComparator); // call @compare (o1,o2)
    }
}

Share this Post