← Quiz: Comparable vs Comparator: all 10 questions
Quiz: Comparable vs Comparator · Question 10 of 10
You cannot change a library class but need to sort its objects by a field. What do you use?
You cannot change a library class but need to sort its objects by a field. What do you use?
- A.Make the class implement Comparable
- B.Write a Comparator outside the class
- C.Override equals
- D.Use a HashSet
Answer & explanation
Correct answer: Write a Comparator outside the class
A Comparator sorts any class without changing its source code.
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 →