Lesson 33 of 34

Sorting, Comparators and Lambdas

10 min 35 XP

Collections.sort() sorts numbers and Strings. To sort objects you need a Comparator. A lambda like (a, b) -> ... writes a small method in one line.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Streams let you filter and transform collections in a few words. Modern Java interviews ask about them often.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

Read n, then n lines of name and score. Print them sorted by score, highest first; for equal scores, by name A to Z. Input: 3 Ram 80 Asha 95 Bala 80 Output: Asha 95 Bala 80 Ram 80

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.