Java Comparable and Comparator – Complete Guide with Examples


Learn how to sort objects in Java using Comparable and Comparator interfaces, their differences, methods, and practical examples for custom sorting of collections.

Comparable and Comparator in Java – Complete Detailed Tutorial

In Java, sorting objects requires defining order. The Comparable and Comparator interfaces provide ways to define natural and custom ordering for objects.

1. Comparable Interface

  1. Defines natural ordering for objects
  2. Part of java.lang package
  3. Used with Collections.sort() or Arrays.sort()
  4. Implements compareTo() method

Syntax:


public int compareTo(T obj)
  1. Returns negative → current object < obj
  2. Returns zero → current object == obj
  3. Returns positive → current object > obj

Example – Using Comparable


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Student implements Comparable<Student> {
String name;
int age;

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

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

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

public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));

Collections.sort(students); // uses compareTo()
System.out.println("Sorted by age: " + students);
}
}

Output:


Sorted by age: [Bob (20), Alice (22), Charlie (25)]

Key Points:

  1. Comparable is implemented by the class itself
  2. Defines natural ordering
  3. Only one sorting sequence possible

2. Comparator Interface

  1. Defines custom ordering for objects
  2. Part of java.util package
  3. Can be separate class or anonymous class
  4. Implements compare() method

Syntax:


public int compare(T o1, T o2)
  1. Returns negative → o1 < o2
  2. Returns zero → o1 == o2
  3. Returns positive → o1 > o2

Example – Using Comparator


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

class Student {
String name;
int age;

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

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

class NameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name); // ascending order by name
}
}

public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));

Collections.sort(students, new NameComparator());
System.out.println("Sorted by name: " + students);
}
}

Output:


Sorted by name: [Alice (22), Bob (20), Charlie (25)]

Key Points:

  1. Comparator is a separate class
  2. Can define multiple sorting sequences
  3. Useful when class cannot implement Comparable

3. Comparable vs Comparator

FeatureComparableComparator
LocationImplemented by class itselfSeparate class or lambda
MethodcompareTo()compare()
Multiple sort sequencesNoYes
Packagejava.langjava.util
SortingNatural orderCustom order

4. Key Points

  1. Use Comparable for default/natural sorting
  2. Use Comparator for custom or multiple sort criteria
  3. Works with Collections.sort(), Arrays.sort()

5. Summary

  1. Comparable: class defines its own natural ordering
  2. Comparator: external class defines custom ordering
  3. Both are essential for sorting Java objects efficiently