MediumExperience level: 3-5 years1 min read

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

Reviewed by Gurusankar M.

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.

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

java
orders.sort(
    Comparator.comparing(Order::priority)
              .thenComparing(Order::createdAt,
                             Comparator.reverseOrder())
              .thenComparing(Order::id));
Output
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

  1. Why should compareTo be consistent with equals?
  2. How do you reverse only one field in a chain?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.