Answer
Comparable vs Comparator in Java
Comparable — Natural Ordering (Built into Class)
Java
import java.util.*;
class Employee implements Comparable<Employee> {
int id;
String name;
double salary;
Employee(int id, String name, double salary) {
this.id = id; this.name = name; this.salary = salary;
}
// Define natural ordering — sort by ID ascending
@Override
public int compareTo(Employee other) {
return this.id - other.id; // negative=this before other, 0=equal, positive=other before this
}
@Override
public String toString() {
return "[" + id + "] " + name + " ($" + salary + ")";
}
}
public class ComparableDemo {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(3, "Charlie", 75000));
employees.add(new Employee(1, "Alice", 90000));
employees.add(new Employee(2, "Bob", 80000));
Collections.sort(employees); // uses compareTo()
System.out.println("Sorted by ID (natural order):");
employees.forEach(System.out::println);
}
}
Output
CODE
Sorted by ID (natural order):
[1] Alice ($90000.0)
[2] Bob ($80000.0)
[3] Charlie ($75000.0)
Comparator — Custom/Multiple Orderings (External)
Java
import java.util.Comparator;
public class ComparatorDemo {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(Arrays.asList(
new Employee(3, "Charlie", 75000),
new Employee(1, "Alice", 90000),
new Employee(2, "Bob", 80000)
));
// Sort by name alphabetically
Comparator<Employee> byName = Comparator.comparing(e -> e.name);
employees.sort(byName);
System.out.println("By name:");
employees.forEach(System.out::println);
// Sort by salary descending
employees.sort(Comparator.comparingDouble((Employee e) -> e.salary).reversed());
System.out.println("\nBy salary (descending):");
employees.forEach(System.out::println);
// Sort by salary, then by name (chained)
employees.sort(
Comparator.comparingDouble((Employee e) -> e.salary)
.thenComparing(e -> e.name)
);
System.out.println("\nBy salary then name:");
employees.forEach(System.out::println);
}
}
Output
CODE
By name:
[1] Alice ($90000.0)
[2] Bob ($80000.0)
[3] Charlie ($75000.0)
By salary (descending):
[1] Alice ($90000.0)
[2] Bob ($80000.0)
[3] Charlie ($75000.0)
By salary then name:
[3] Charlie ($75000.0)
[2] Bob ($80000.0)
[1] Alice ($90000.0)
Comparable vs Comparator Summary
| Comparable | Comparator | |
|---|---|---|
| Package | java.lang | java.util |
| Method | compareTo(T o) | compare(T o1, T o2) |
| Implemented in | The class itself | Separate class or lambda |
| Sort orders | One (natural) | Many (multiple Comparators) |
| Modifying class | Required | Not required |
| Usage | Collections.sort(list) | Collections.sort(list, comp) |
Lambda Comparators (Java 8+)
Java
// Sort strings by length
List<String> tools = Arrays.asList("Selenium", "TestNG", "Cucumber", "Maven");
tools.sort((a, b) -> a.length() - b.length());
System.out.println("By length: " + tools);
// → [Maven, TestNG, Cucumber, Selenium]
// Using Comparator.comparingInt
tools.sort(Comparator.comparingInt(String::length).reversed());
System.out.println("By length desc: " + tools);
// → [Selenium, Cucumber, TestNG, Maven]
