← Quiz: Comparable vs Comparator: all 10 questions
Quiz: Comparable vs Comparator · Question 7 of 10
How do you sort a List<String containing nulls with nulls at the end?
How do you sort a List<String> containing nulls with nulls at the end?
- A.list.sort(null)
- B.list.sort(Comparator.nullsLast(Comparator.naturalOrder()))
- C.Collections.sort(list)
- D.list.sort(Comparator.reverseOrder())
Answer & explanation
Correct answer: list.sort(Comparator.nullsLast(Comparator.naturalOrder()))
nullsLast wraps another comparator and places nulls after all other values.
Can you get the rest right without peeking?
Take the timed Quiz: Comparable vs Comparator quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →