Comparator vs Comparable in Java are two keyword used to compare two objects. They are used to check whether one object is less than, equal to, or greater than another object.
Table of Contents
Difference Between Comparator and Comparable in Java?
Comparator is used to sort collections of objects in a custom order, rather than the natural order of the objects themselves. For example, you may want to sort a collection of employees by their salary, rather than their names. whereas, Comparable is used to sort collections of objects in their natural order. For example, you may want to sort a collection of employees by their names.
Comparator Method
import java.util.Comparator;
public class MyClass {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public static Comparator<MyClass> IdComparator = new Comparator<MyClass>() {
public int compare(MyClass o1, MyClass o2) {
return o1.getId() - o2.getId();
}
};
public static Comparator<MyClass> NameComparator = new Comparator<MyClass>() {
public int compare(MyClass o1, MyClass o2) {
return o1.getName().compareTo(o2.getName());
}
};
public static void main(String[] args) {
MyClass object1 = new MyClass(1, "John Doe");
MyClass object2 = new MyClass(2, "Jane Smith");
MyClass object3 = new MyClass(3, "Bob Johnson");
MyClass[] objects = {object1, object2, object3};
System.out.println("Sorting by ID:");
java.util.Arrays.sort(objects, MyClass.IdComparator);
for (MyClass object : objects) {
System.out.println(object.getId() + " " + object.getName());
}
System.out.println("Sorting by Name:");
java.util.Arrays.sort(objects, MyClass.NameComparator);
for (MyClass object : objects) {
System.out.println(object.getId() + " " + object.getName());
}
}
}
Output

Comparable Methods
compare():
public class MyClass implements Comparable<MyClass> {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int compareTo(MyClass other) {
return this.id - other.id;
}
public static void main(String[] args) {
MyClass object1 = new MyClass(1, "John Doe");
MyClass object2 = new MyClass(2, "Jane Smith");
MyClass object3 = new MyClass(3, "Bob Johnson");
MyClass[] objects = {object1, object2, object3};
System.out.println("Sorting by ID:");
java.util.Arrays.sort(objects);
for (MyClass object : objects) {
System.out.println(object.getId() + " " + object.getName());
}
}
}
Output

Equals():
public class MyClass implements Comparable<MyClass> {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int compareTo(MyClass other) {
return this.id - other.id;
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof MyClass)) {
return false;
}
MyClass otherMyClass = (MyClass) other;
return this.id == otherMyClass.id && this.name.equals(otherMyClass.name);
}
public static void main(String[] args) {
MyClass object1 = new MyClass(1, "John Doe");
MyClass object2 = new MyClass(2, "Jane Smith");
MyClass object3 = new MyClass(3, "Bob Johnson");
MyClass object4 = new MyClass(1, "John Doe");
System.out.println("object1 and object2 are equal: " + object1.equals(object2));
System.out.println("object1 and object3 are equal: " + object1.equals(object3));
System.out.println("object1 and object4 are equal: " + object1.equals(object4));
}
}
Output

Conclusion
Easily sort your way to success with Comparator and Comparable in Java. These powerful interfaces make coding a breeze and elevate your sorting game to new heights.