Explain the difference between Comparable and Comparator in Java
๐ก Concept: Comparable vs Comparator
Comparable and Comparator are Java interfaces used for sorting objects, but they differ in implementation and usage.
๐ Quick Intro
Comparable defines natural ordering by implementing compareTo(), while Comparator allows custom ordering through compare().
๐ง Analogy
Think of Comparable as a default sorting rule built into an object, while Comparator is a separate rulebook you can apply whenever needed.
๐ง Technical Explanation
- Comparable interface requires implementing compareTo() method.
- Comparator interface requires implementing compare() method.
- Comparable sorts based on natural order inside the class.
- Comparator provides external sorting logic.
- Comparator can be used to sort by multiple fields.
- Comparator allows multiple different sorting sequences.
๐ฏ Use Cases
- โ Use Comparable to define default sort order.
- โ Use Comparator to define alternative sorting criteria.
- โ Useful in sorting collections and arrays.
๐ป Code Example: Comparable vs Comparator
import java.util.*;
class Person implements Comparable {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
// Natural ordering by name
public int compareTo(Person other) {
return this.name.compareTo(other.name);
}
}
class AgeComparator implements Comparator {
public int compare(Person p1, Person p2) {
return Integer.compare(p1.age, p2.age);
}
}
public class CompareExample {
public static void main(String[] args) {
List people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
// Sort by natural order (name)
Collections.sort(people);
// Sort by age using Comparator
Collections.sort(people, new AgeComparator());
}
}

โ Interview Q&A
Q1: What is Comparable?
A: Interface for natural ordering with compareTo().
Q2: What is Comparator?
A: Interface for custom ordering with compare().
Q3: Can a class implement both?
A: Yes, but uncommon.
Q4: Which method does Comparable require?
A: compareTo().
Q5: Which method does Comparator require?
A: compare().
Q6: Can Comparator be used to sort by multiple fields?
A: Yes.
Q7: What is natural ordering?
A: Default sorting order defined by Comparable.
Q8: Can Comparator be anonymous class?
A: Yes.
Q9: Does Comparable modify the class?
A: Yes, by implementing interface.
Q10: Is Comparator reusable?
A: Yes, can be used with different classes.
๐ MCQs
Q1. What does Comparable interface do?
- Defines natural ordering
- Defines custom ordering
- No ordering
- None
Q2. What does Comparator interface do?
- Defines natural ordering
- Defines custom ordering
- No ordering
- None
Q3. Which method is in Comparable?
- compare()
- compareTo()
- equals()
- hashCode()
Q4. Which method is in Comparator?
- compare()
- compareTo()
- equals()
- hashCode()
Q5. Can a class implement both Comparable and Comparator?
- Yes
- No
- Sometimes
- Never
Q6. What is natural ordering?
- Default sort order
- Custom sort order
- Random order
- None
Q7. Can Comparator be anonymous class?
- Yes
- No
- Sometimes
- Never
Q8. Does Comparable modify the class?
- Yes
- No
- Sometimes
- Never
Q9. Is Comparator reusable?
- Yes
- No
- Sometimes
- Never
Q10. Can Comparator sort by multiple fields?
- Yes
- No
- Sometimes
- Never
๐ก Bonus Insight
Understanding Comparable and Comparator helps write flexible and reusable sorting logic in Java applications.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!