Medium👤 3-5 years 1 min read

Comparable vs Comparator — how do you sort domain objects by multiple fields?

Asked inAmazonCognizantDeloitte
#comparable#comparator#sorting#streams
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

Comparable vs Comparator — how do you sort domain objects by multiple fields?

🧠Mind Map Answer

Remember It Faster

Comparablenatural order, one per class
Comparatormany orders, external, composable
Multi-fieldcomparing().thenComparing()

⌨️Hands-on Keyboard

Learn by Doing

java
orders.sort(
    Comparator.comparing(Order::priority)
              .thenComparing(Order::createdAt,
                             Comparator.reverseOrder())
              .thenComparing(Order::id));
Output
sorted by priority, then newest, then id

🔥What If?

Think Beyond the Expected

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 ordercomparing/thenComparingnullsFirst/nullsLastconsistency 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

  • 1Why should compareTo be consistent with equals?
  • 2How do you reverse only one field in a chain?
  • 3How does this interact with TreeMap/TreeSet ordering?

🧩Related Technologies

TreeMap/TreeSetStream.sortedCollections.sort

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions