🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.list.sort(null)
  2. B.list.sort(Comparator.nullsLast(Comparator.naturalOrder()))
  3. C.Collections.sort(list)
  4. 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 →