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
- Maps & Frequency Counting
- Comparable vs Comparator
- Sorting/Searching with Collections