Advanced Java October 06 ,2025

Comparable vs Comparator in Java

Understanding Sorting Mechanisms with Real Examples

Introduction

Sorting is one of the most common operations when working with collections in Java.
Java provides two interfaces for custom sorting:

  • Comparable — for natural ordering of objects.
  • Comparator — for custom or multiple orderings.

Understanding these interfaces is essential for working with sorted collections, priority queues, and other data structures.

This blog will cover:

  • What Comparable and Comparator are.
  • Key differences.
  • How to implement each.
  • Practical examples with custom classes.
  • Advantages and limitations of each approach.

1. Comparable Interface

Definition

The Comparable interface is part of the java.lang package.
It is used to define natural ordering for objects of a class.

Signature:

public interface Comparable {
    int compareTo(T o);
}

Rules for compareTo:

  • Return a negative integer if this object is less than the other.
  • Return zero if equal.
  • Return a positive integer if greater.

Example: Comparable Implementation

Let’s create a Student class sorted by marks.

class Student implements Comparable {
    String name;
    int marks;

    public Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }

    @Override
    public int compareTo(Student other) {
        return this.marks - other.marks; // ascending order by marks
    }

    @Override
    public String toString() {
        return name + " → " + marks;
    }
}

Testing Comparable

import java.util.*;

public class ComparableTest {
    public static void main(String[] args) {
        List students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 75));
        students.add(new Student("Charlie", 90));

        Collections.sort(students);

        for (Student s : students) {
            System.out.println(s);
        }
    }
}

Output:

Bob → 75
Alice → 85
Charlie → 90

Explanation:

  • The compareTo method defined natural ordering for Student.

2. Comparator Interface

Definition

The Comparator interface is part of the java.util package.
It is used for custom ordering, especially when:

  • Multiple sort criteria are needed.
  • We don’t want to modify the class itself.
  • Sorting external or library classes.

Signature:

public interface Comparator {
    int compare(T o1, T o2);
}

Example: Comparator Implementation

Sorting students by name instead of marks.

class NameComparator implements Comparator {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}

Testing Comparator

import java.util.*;

public class ComparatorTest {
    public static void main(String[] args) {
        List students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 75));
        students.add(new Student("Charlie", 90));

        Collections.sort(students, new NameComparator());

        for (Student s : students) {
            System.out.println(s);
        }
    }
}

Output:

Alice → 85
Bob → 75
Charlie → 90

Explanation:

  • Comparator allows external control of sorting order.

3. Differences Between Comparable and Comparator

FeatureComparableComparator
Packagejava.langjava.util
MethodcompareTo(T o)compare(T o1, T o2)
Sorting TypeNatural orderingCustom ordering
Modification of ClassRequiredNot required
Multiple SortingNot possible directlyPossible
Example UsageCollections.sort(list)Collections.sort(list, comparator)

4. Practical Example with Multiple Comparators

Let’s sort students by marks descending and then by name ascending.

class MarksComparator implements Comparator {
    @Override
    public int compare(Student s1, Student s2) {
        return s2.marks - s1.marks; // descending marks
    }
}

class NameComparatorAsc implements Comparator {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name); // ascending name
    }
}

Testing:

students.sort(new MarksComparator().thenComparing(new NameComparatorAsc()));

5. Comparable vs Comparator in Java 8+ with Lambdas

Java 8 introduced lambdas, making comparators concise.

Example:

students.sort((s1, s2) -> s1.name.compareTo(s2.name));
students.sort(Comparator.comparing(Student::getMarks).reversed());

6. Advantages of Comparable

  • Simple to use.
  • Natural ordering is built into the class.
  • Works directly with Collections.sort().

Limitations:

  • Only one sorting criterion allowed.
  • Requires modifying the class.

7. Advantages of Comparator

  • Supports multiple sorting criteria.
  • Does not require modifying the class.
  • Works with any class, even library classes.

Limitations:

  • More verbose without lambdas.
  • Slightly more overhead compared to Comparable.

8. Real-World Use Cases

  • Sorting custom objects in different orders.
  • External sorting rules for library classes.
  • Multi-field sorting in business applications (e.g., sorting employees by department, then salary).
  • Dynamic sorting in APIs.

9. Summary Table

AspectComparableComparator
Single sortYesNo
Modify classYesNo
Multiple sortsNoYes
External sortingNoYes
Java 8 LambdaLimitedPowerful

Conclusion

Comparable and Comparator in Java provide flexible sorting capabilities:

  • Comparable → class-level natural ordering.
  • Comparator → external, flexible ordering.

Choosing between them depends on the context:

  • If there’s a single natural ordering → use Comparable.
  • If multiple/custom orderings are required → use Comparator.

Understanding both deeply is crucial for writing clean and maintainable Java code that works with sorted collections effectively.

 

Next Blog- Custom Implementation of CyclicBarrier in Java

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com