Given: 11. List list = // more code here 12. Collections.sort(list, new MyComparator()); Which code will sort this list in the opposite order of the sort in line 12?
-
Collections.reverseSort(list, new MyComparator());
-
Collections.sort(list, new MyComparator());
-
Collections.sort(list, new InverseComparator(
-
Collections.sort(list, Collections.reverseOrder(
Collections.reverseOrder() accepts a Comparator and returns a new Comparator that imposes the reverse ordering. When passed to Collections.sort(), it sorts the list in the opposite order of the original comparator. This is the standard way to reverse sort order in Java.
Collections.sort(list, Collections.reverseOrder(...)) is the mechanism intended: Collections.reverseOrder() returns a Comparator that imposes the reverse ordering, and passing it (or a comparator wrapping the original MyComparator) to sort reverses the previous ordering. The other options are invalid: reverseSort doesn't exist in the Collections API, calling sort again with the same MyComparator just reproduces the original order (not its reverse), and InverseComparator isn't a real java.util class. Note the answer text for this option appears truncated in the source data (cut off after the opening parenthesis) — the intended full call is very likely Collections.reverseOrder(new MyComparator()), which correctly reverses the specific ordering from line 12, so despite the truncation the conceptual answer is right.