Comparable vs Comparator in Java
In Java, Comparable and Comparator are interfaces used to sort objects. Both help us define sorting logic for custom objects such as Employee, Student, Product, etc.
The main difference is:
- Comparable: Defines the natural/default sorting order inside the class.
- Comparator: Defines custom sorting orders outside the class.
1. Comparable Interface
The Comparable interface is available in the java.lang package.
It is used to define the default sorting logic inside the class itself.
Example - 1
Sorting employees by salary in ascending order.
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee implements Comparable<Employee> { public int empID; public String empName; public int salary; public Employee(int empID, String empName, int salary) { this.empID = empID; this.empName = empName; this.salary = salary; } public int getEmpID() { return empID; } public String getEmpName() { return empName; } public int getSalary() { return salary; } @Override public int compareTo(Employee o) { return this.salary - o.salary; } } public class ComparableExample { public static void main(String[] args) { List<Employee> emp = new ArrayList<>(); emp.add(new Employee(3, "Sumit", 5433)); emp.add(new Employee(1, "Aumit", 2433)); emp.add(new Employee(2, "Cumit", 3433)); emp.add(new Employee(4, "Bumit", 6433)); Collections.sort(emp); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } } }
Output :
1 - 2433 - Aumit 2 - 3433 - Cumit 3 - 5433 - Sumit 4 - 6433 - Bumit
Example - 2
Sorting employees by salary in descending order.
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee implements Comparable<Employee> { public int empID; public String empName; public int salary; public Employee(int empID, String empName, int salary) { this.empID = empID; this.empName = empName; this.salary = salary; } public int getEmpID() { return empID; } public String getEmpName() { return empName; } public int getSalary() { return salary; } @Override public int compareTo(Employee o) { return o.salary - this.salary; } } public class ComparableExample { public static void main(String[] args) { List<Employee> emp = new ArrayList<>(); emp.add(new Employee(3, "Sumit", 5433)); emp.add(new Employee(1, "Aumit", 2433)); emp.add(new Employee(2, "Cumit", 3433)); emp.add(new Employee(4, "Bumit", 6433)); Collections.sort(emp); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } } }
Output
4 - 6433 - Bumit 3 - 5433 - Sumit 2 - 3433 - Cumit 1 - 2433 - Aumit
Example - 3
Sorting employees by empName in descending order.
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee implements Comparable<Employee> { public int empID; public String empName; public int salary; public Employee(int empID, String empName, int salary) { this.empID = empID; this.empName = empName; this.salary = salary; } public int getEmpID() { return empID; } public String getEmpName() { return empName; } public int getSalary() { return salary; } @Override public int compareTo(Employee o) { return o.empName.compareTo(this.empName); } } public class ComparableExample { public static void main(String[] args) { List<Employee> emp = new ArrayList<>(); emp.add(new Employee(3, "Sumit", 5433)); emp.add(new Employee(1, "Aumit", 2433)); emp.add(new Employee(2, "Cumit", 3433)); emp.add(new Employee(4, "Bumit", 6433)); Collections.sort(emp); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } } }
Output
3 - 5433 - Sumit 2 - 3433 - Cumit 4 - 6433 - Bumit 1 - 2433 - Aumit
2. Comparator Interface
The Comparator interface is available in the java.util package.
It is used to define multiple custom sorting logics without modifying the original class.
It provides the compare() method.
Example -1
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Employee { public int empID; public String empName; public int salary; public Employee(int empID, String empName, int salary) { this.empID = empID; this.empName = empName; this.salary = salary; } public int getEmpID() { return empID; } public String getEmpName() { return empName; } public int getSalary() { return salary; } } public class ComparatorExample { public static void main(String[] args) { List<Employee> emp = new ArrayList<>(); emp.add(new Employee(3, "Sumit", 5433)); emp.add(new Employee(1, "Aumit", 2433)); emp.add(new Employee(2, "Cumit", 3433)); emp.add(new Employee(4, "Bumit", 6433)); // sort employee empID by ascending order Collections.sort(emp, Comparator.comparingInt(e -> e.getEmpID())); System.out.print("\n sort employee empID by ascending order \n"); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } // sort employee empID by descending order Collections.sort(emp, Comparator.comparingInt((Employee e) -> e.getEmpID()).reversed()); System.out.print("\n sort employee empID by descending order \n"); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } // sort employee empName by ascending order Collections.sort(emp, Comparator.comparing(e -> e.getEmpName())); System.out.print("\n sort employee empID by descending order \n"); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } // sort employee empName by descending order Collections.sort(emp, Comparator.comparing((Employee e) -> e.getEmpName()).reversed()); System.out.print("\n sort employee empName by descending order \n"); for (Employee e : emp) { System.out.print(e.empID + " - " + e.salary + " - " + e.empName + "\n"); } } }
Output :
sort employee empID by ascending order 1 - 2433 - Aumit 2 - 3433 - Cumit 3 - 5433 - Sumit 4 - 6433 - Bumit sort employee empID by descending order 4 - 6433 - Bumit 3 - 5433 - Sumit 2 - 3433 - Cumit 1 - 2433 - Aumit sort employee empID by descending order 1 - 2433 - Aumit 4 - 6433 - Bumit 2 - 3433 - Cumit 3 - 5433 - Sumit sort employee empName by descending order 3 - 5433 - Sumit 2 - 3433 - Cumit 4 - 6433 - Bumit 1 - 2433 - Aumit
Difference Between Comparable and Comparator
The table below demonstrates the difference between comparable and comparator in Java.
Features | Comparable | Comparator |
|---|---|---|
Definition | It defines natural ordering within the class. | It defines external or custom sorting logic. |
Method | compareTo() | compare() |
Implementation | It is implemented in the class. | It is implemented in a separate class. |
Sorting Criteria | Natural order sorting | Custom order sorting |
Usage | It is used for a single sorting order. | It is used for multiple sorting orders. |

No comments:
Post a Comment