Sorting and comparison
On this page 8 topics
Overview
Topics
- Comparable
- Comparator
- sorting collections
Learn first
40 min of reading · 2 h of practice (estimate)
Learning outcomes
- Implement
Comparablefor a natural ordering. - Build
Comparators, including multi-key ones withcomparingandthenComparing. - Sort lists and use sorted collections (
TreeMap,TreeSet) with custom ordering.
Lessons
Lab and exercises
Lab
BEG-032-LAB1 · Leaderboard
Outcomes: 1–3 · Time: ~1 h Sort players by score descending, then name ascending, and show the top N. Also keep players in a TreeSet using the same comparator. Acceptance: ties ordered correctly.
Exercises (assessment: three exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-032-EX01 | 1 | Make a Book (title, year) implement Comparable<Book> by year; sort a list of books. |
| BEG-032-EX02 | 2, 3 | Build a comparator for Book: year descending, then title ascending; sort with it. |
| BEG-032-EX03 | 3 | Put books in a TreeSet using a title comparator. Add two books with the same title and different years; explain the size. |
Show solutions
- EX01
public int compareTo(Book o){ return Integer.compare(year, o.year); }thenCollections.sort(books);. - EX02
Comparator.comparingInt(Book::getYear).reversed().thenComparing(Book::getTitle). - EX03 The set has size 1 for the duplicated title: the comparator returns 0, so the set treats them as the same element. This is why ordering must match what "equal" means for the set.
- Subtraction-overflow mistake is covered in demo L04 and quiz Q09.
Quiz
Outcomes: O1 Comparable · O2 Comparator · O3 sorting and sorted collections.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Three exercises BEG-032-EX01 to EX03 plus the lab. Pass: all three exercises correct and tied scores ordered by name in the lab.
Common mistakes
Subtraction-based compare (overflow); ordering inconsistent with equals.
Assessment
3 exercises.
Career relevance
Sorting in APIs (JNR-019) and SQL ORDER BY (BEG-050).