← Quiz: Counting Sort and Custom Sorting: all 13 questions
Quiz: Counting Sort and Custom Sorting · Question 12 of 13
Which Java comparator safely sorts an Integer[] from smallest to largest, even with very large values?
Which Java comparator safely sorts an Integer[] from smallest to largest, even with very large values?
- A.(a, b) -> a - b
- B.(a, b) -> Integer.compare(a, b)
- C.(a, b) -> b - a
- D.(a, b) -> a > b ? 1 : 0
Answer & explanation
Correct answer: (a, b) -> Integer.compare(a, b)
a - b can overflow, and a version that never returns a negative number breaks the comparator contract.
Can you get the rest right without peeking?
Take the timed Quiz: Counting Sort and Custom Sorting quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →