0Pricing
Java Academy · Lesson

Comparable vs Comparator

Learn the difference between Comparable and Comparator for custom sorting. Practice compareTo and external Comparators.

Comparable vs Comparator is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Sorting needs rules. Comparable and Comparator are two ways to define them:

  • Comparable: class implements compareTo.
  • Comparator: separate object passed to sort().

Comparable concept

Comparable lets a class define its natural order. Implement compareTo so Collections.sort knows how to order items.

Code: Comparable

Here Person implements Comparable and sorts by age. This defines its natural order.

public class Main {
  static class Person implements Comparable<Person> {
    String name;
    int age;
    Person(String n, int a) { name = n; age = a; }
    public int compareTo(Person o) {
      return this.age - o.age; // order by age
    }
    public String toString() { return name + "(" + age + ")"; }
  }
  public static void main(String[] args) {
    java.util.List<Person> list = new java.util.ArrayList<>();
    list.add(new Person("Ada",30));
    list.add(new Person("Bob",25));
    java.util.Collections.sort(list);
    System.out.println(list); // [Bob(25), Ada(30)]
  }
}

Comparator concept

Comparator is external. It can sort the same class in different ways, e.g., by name instead of age.

Code: Comparator

This Comparator sorts by name. Different Comparators can be reused on the same class.

public class Main {
  static class Person {
    String name; int age;
    Person(String n,int a){ name=n; age=a; }
    public String toString(){ return name+"("+age+")"; }
  }
  public static void main(String[] args) {
    java.util.List<Person> list = new java.util.ArrayList<>();
    list.add(new Person("Ada",30));
    list.add(new Person("Bob",25));

    java.util.Comparator<Person> byName = (p1,p2) -> p1.name.compareTo(p2.name);
    java.util.Collections.sort(list, byName);
    System.out.println(list); // [Ada(30), Bob(25)]
  }
}

Code: reverse Comparator

Comparators can define any rule, such as descending order here.

public class Main {
  public static void main(String[] args) {
    java.util.List<Integer> nums = new java.util.ArrayList<>();
    nums.add(5); nums.add(1); nums.add(3);

    java.util.Comparator<Integer> desc = (a,b) -> b - a;
    java.util.Collections.sort(nums, desc);
    System.out.println(nums); // [5,3,1]
  }
}

Comparable vs Comparator check

Quick check: What is the main difference between Comparable and Comparator?

Recap

Recap: Comparable defines a natural order inside the class. Comparator is separate and can create many sorting strategies.

Frequently asked questions

Is the “Comparable vs Comparator” lesson free?

Yes — the full text of “Comparable vs Comparator” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Comparable vs Comparator”?

Learn the difference between Comparable and Comparator for custom sorting. Practice compareTo and external Comparators. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Comparable vs Comparator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Maps & Frequency Counting
  2. Comparable vs Comparator
  3. Sorting/Searching with Collections
← Back to Java Academy