Writing
Comparators
Learn to define custom sorting logic in Java by implementing the Comparator interface: control how objects are ordered in collections and arrays.
Why Do We Need Custom Sorting?
Java's Collections.sort() and Arrays.sort() know how to sort numbers and strings by default. But what about sorting a list of Student objects by GPA? Or a list of Song objects by duration?
Think of a Comparator as a judge at a competition: it looks at two contestants and decides who ranks higher.
The Comparator Interface
java.util.Comparator<T> is a functional interface with one essential method:
a should come before ba and b are equal in ordera should come after bHow compare() Drives Sorting
The compare() Contract
Your compare method must follow a simple rule: return an integer whose sign indicates order.
Comparable vs Comparator
| Comparable | Comparator | |
|---|---|---|
| Package | java.lang | java.util |
| Method | compareTo(T o) |
compare(T a, T b) |
| Where defined | Inside the class itself | In a separate class or lambda |
| Sort orders | One "natural" order | Unlimited custom orders |
| Modifies class? | Yes | No |
How to Write a Comparator
Student) and the field to sort by (e.g., gpa).Comparator<Student>, or write a lambda expression.compare(Student a, Student b). Return a negative, zero, or positive integer.Collections.sort(list, comparator) or Arrays.sort(arr, comparator).Example 1: Named Class
Sort a list of students by age in ascending order:
Example 2: Lambda Shorthand
The same sort using a lambda expression: no separate class needed.
Example 3: Sorting by Name
Sort students alphabetically by last name. Strings already implement Comparable, so delegate:
For reverse alphabetical (Z to A), simply swap a and b:
Example 4: Multi-Key Sorting
Sort by GPA descending, then break ties by last name ascending:
Ascending vs Descending
Safer Comparison with Integer.compare
The subtraction trick a - b can overflow with extreme values. Java provides safe alternatives:
Integer.MAX_VALUE or MIN_VALUE.Plugging In Your Comparator
Common Mistakes
Comparator<Object> instead of Comparator<Student> forces casting and loses type safety.b - a when you wanted ascending, or a - b when you wanted descending.a - b with extreme int values. Use Integer.compare(a, b) instead.Summary Cheat Sheet
| Goal | Code Pattern |
|---|---|
| Ascending (int) | Integer.compare(a.x, b.x) |
| Descending (int) | Integer.compare(b.x, a.x) |
| Ascending (double) | Double.compare(a.x, b.x) |
| Alphabetical (String) | a.s.compareTo(b.s) |
| Reverse alphabetical | b.s.compareTo(a.s) |
| Multi-key | Compare primary, if 0 compare secondary |
Question 1 of 5
What does compare(a, b) return when a should come before b?
Question 2 of 5
Which expression sorts students by age in descending order?
b.getAge() - a.getAge(). When b is larger, the result is positive, pushing a after b.Question 3 of 5
What is the key difference between Comparable and Comparator?
Question 4 of 5
Consider this comparator:
(a, b) -> { int c = Double.compare(b.getGpa(), a.getGpa()); if (c != 0) return c; return a.getName().compareTo(b.getName()); }
How does this comparator sort students?
b.getGpa() comes first). When GPAs are equal, it falls through to name ascending using compareTo.Question 5 of 5
Why is Integer.compare(a, b) preferred over a - b?
a is near Integer.MAX_VALUE and b is negative, a - b can overflow and produce a wrong sign. Integer.compare() handles this safely.