We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Collections.sort(Collection, Comparator) and Collections.sort(Collection) are overloaded method. And we need to use the first form here, which will take a Comparator as the second parameter, since reference type does not have a natural ordering.
The Comparator interface consists of a single method.
The compare method compares its two arguments, returning a negative integer, 0, or a positive integer depending on whether the first argument is less than, equal to, or greater than the second.
As we need decreasing order, therefore the comperator should return a positive value when the order appears to be increasing, so that it will change the order and make it decreasing again. Hence, b.score - a.score.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Comparator
You are viewing a single comment's thread. Return to all comments →
Collections.sort(Collection, Comparator) and Collections.sort(Collection) are overloaded method. And we need to use the first form here, which will take a Comparator as the second parameter, since reference type does not have a natural ordering.
The Comparator interface consists of a single method.
The compare method compares its two arguments, returning a negative integer, 0, or a positive integer depending on whether the first argument is less than, equal to, or greater than the second.
As we need decreasing order, therefore the comperator should return a positive value when the order appears to be increasing, so that it will change the order and make it decreasing again. Hence, b.score - a.score.