0Pricing
Java Academy · Lesson

Comparable vs Comparator

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

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.

All lessons in this course

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