Java Comparable vs Comparator: Natural & Custom Object Sorting

When sorting custom objects (e.g. Student, Product, Employee), the JVM needs to know which attributes dictate ordering (e.g. sort by GPA, by Age, or by Name). Java provides two interfaces: Comparable and Comparator.

Side-by-Side Comparison Matrix

Feature Comparable<T> Comparator<T>
Package java.lang (Imported automatically) java.util
Method to Implement public int compareTo(T other) public int compare(T o1, T o2)
Sorting Scope Single natural sorting order Multiple sorting criteria
Modifies Class? Yes (Original class must implement it) No (Can be written as external lambda)

1. Comparable: Natural Ordering

public class Student implements Comparable<Student> {
    int rollNo;
    String name;
    double gpa;

    public Student(int rollNo, String name, double gpa) {
        this.rollNo = rollNo;
        this.name = name;
        this.gpa = gpa;
    }

    // Natural ordering: Sorted ascending by rollNo
    @Override
    public int compareTo(Student other) {
        return Integer.compare(this.rollNo, other.rollNo);
    }
}

2. Modern Comparator with Lambdas (Java 8+)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ComparatorDemo {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(105, "Vikram", 3.4));
        students.add(new Student(101, "Aarav", 3.9));
        students.add(new Student(108, "Sneha", 3.7));

        // Sort 1: Natural order via Comparable (rollNo)
        Collections.sort(students);

        // Sort 2: Custom sorting by Name using Lambda:
        students.sort((s1, s2) -> s1.name.compareTo(s2.name));

        // Sort 3: Modern Comparator chaining: Highest GPA first
        students.sort(Comparator.comparingDouble((Student s) -> s.gpa).reversed());
    }
}

Next Step

Now let us explore the algorithms in the java.util.Collections utility class. Proceed to: Collections Utility Class →

Frequently Asked Questions (FAQ)

Q1. What is the difference between Comparable and Comparator in Java?
Comparable (in java.lang) modifies the actual class to define a single 'natural' sorting order by overriding compareTo(). Comparator (in java.util) is an external class or lambda defining multiple customizable sorting strategies without touching the original class.
Q2. What do the return values of compareTo() and compare() signify?
A negative integer (< 0) indicates the first object is smaller than the second. Zero (0) indicates both objects are equal. A positive integer (> 0) indicates the first object is greater than the second.
Q3. How do modern Java 8+ lambdas simplify writing Comparators?
Java 8 allows writing inline Comparators concisely using lambda syntax or method references: 'Comparator.comparing(Student::getName)' or 'Comparator.comparingDouble(Employee::getSalary).reversed()'.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Collections.sort() →

Sorting lists with comparators.

Java TreeSet →

Sorting elements via Comparator.

Java TreeMap →

Sorting keys via Comparator.

Lambda Expressions →

Writing lambda comparator functions.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!