Comparable vs Comparator — how do you sort domain objects by multiple fields?
Reviewed by Gurusankar M.
Short Answer
Comparable defines a single natural order inside the class (compareTo). Comparator defines external, swappable orders. For multi-field sorting use Comparator.comparing(...).thenComparing(...) — clean and null-safe.
On this page
Concept Made Simple
Coffee Chat Question
“Comparable vs Comparator — how do you sort domain objects by multiple fields?”
Remember It Faster
Mind Map Answer
- Comparable
- natural order, one per class
- Comparator
- many orders, external, composable
- Multi-field
- comparing().thenComparing()
Learn by Doing
Hands-on Keyboard
orders.sort(
Comparator.comparing(Order::priority)
.thenComparing(Order::createdAt,
Comparator.reverseOrder())
.thenComparing(Order::id));sorted by priority, then newest, then id
Think Beyond the Expected
What If?
Your compareTo throws when a field is null — how do you fix it cleanly?
Use Comparator.comparing(Order::name, Comparator.nullsLast(naturalOrder())) so nulls sort predictably instead of NPE-ing inside compareTo.
Real World
Sorting a results grid by status, then date, then id is a one-liner with thenComparing — replacing brittle hand-written compareTo logic that breaks on the next field added.
Interviewer's Expectation
Keywords they're listening for:
- natural vs external order
- comparing/thenComparing
- nullsFirst/nullsLast
- consistency with equals
Common Mistakes
- Returning a.field - b.field (int overflow) instead of Integer.compare
- Ignoring nulls in comparators
- Inconsistent compareTo vs equals breaking TreeSet
Best Practices
- Use Comparator.comparing/thenComparing for readability
- Use Integer.compare/Long.compare, never subtraction
- Handle nulls explicitly with nullsFirst/nullsLast
Follow-up Questions
- Why should compareTo be consistent with equals?
- How do you reverse only one field in a chain?
- How does this interact with TreeMap/TreeSet ordering?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: OOP (Core Java) Interview question: "Comparable vs Comparator — how do you sort domain objects by multiple fields?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair