Java Sorting: Using Comparable and Comparator Together

Imran AnsariImran Ansari
2 min read

Table of contents

Introduction

Sorting in Java is easy when you're dealing with simple data types. But what happens when you want to sort a list of custom objects like Student, Employee, or Book? This is where Comparable and Comparator come in.

In this article, you’ll learn:

  • How to define natural sorting using Comparable

  • How to apply custom sorting with Comparator

  • How to combine both in one Java class

The Problem

You have a list of students with the following details:

  • ID

  • Name

  • Age

Now, you want to:

  1. Sort them by ID (natural order)

  2. Sort them by age

  3. Sort them by name

Let’s implement this using both interfaces.

Code Example: Comparable + Comparator

import java.util.*;

// Student class with natural ordering by id
class Student implements Comparable<Student> {
    int id;
    String name;
    int age;

    Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    // Natural ordering: by id
    public int compareTo(Student other) {
        return Integer.compare(this.id, other.id);
    }
}


// Comparator to sort by Age
class SortByAge implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return Integer.compare(s1.age, s2.age);
    }
}

// Comparator to sort by Name
class SortByName implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}

class TestSort {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(77, "Keerti", 18));
        students.add(new Student(103, "Piyush", 20));
        students.add(new Student(85, "Lucky", 19));

        System.out.println("Sort by ID (Comparable):");
        Collections.sort(students);  // uses compareTo()
        for (Student s : students)
            System.out.println(s.id + " " + s.name + " " + s.age);

        System.out.println("\nSort by Age (Comparator):");
        Collections.sort(students, new SortByAge());
        for (Student s : students)
            System.out.println(s.id + " " + s.name + " " + s.age);

        System.out.println("\nSort by Name (Comparator):");
        Collections.sort(students, new SortByName());
        for (Student s : students)
            System.out.println(s.id + " " + s.name + " " + s.age);
    }
}

Output:

Summary: Comparable vs Comparator

Feature

Comparable

Comparator

Purpose

Defines natural order

Defines custom order

Method

compareTo(T o)

compare(T o1, T o2)

Where defined

Inside the class

Outside the class

Flexibility

Only one sort rule

Multiple sorting rules can be written

Usage

Collections.sort(list)

Collections.sort(list, comparator)


🏁 Conclusion

  • Use Comparable when your class has a default sorting order (like ID).

  • Use Comparator when you need additional or flexible sorting (like by name or age).

  • You can combine both in one class to keep your code clean and reusable.

0
Subscribe to my newsletter

Read articles from Imran Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Imran Ansari
Imran Ansari