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.reverseOrder does exactly what it says, returns the Comparator that is passed in which performs its operation in reverse.
Comparator.comparing takes a function to use for the actual comparison and returns a Comparator.
In our case we want to compare BigDecimals so we can pass in a method reference which invokes BigDecimal's String constructor. This is the same as saying:
Comparator.comparing(y->newBigDecimal(y))
Since we converted from String to BigDecimal using this constructor the comparator will now use BigDecimal's compareTo method to sort the Array.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java BigDecimal
You are viewing a single comment's thread. Return to all comments →
For sure -
Arrays.sort takes an array and a Comparator. We pass in the array x, and
Collections.reverseOrder does exactly what it says, returns the Comparator that is passed in which performs its operation in reverse.
Comparator.comparing takes a function to use for the actual comparison and returns a Comparator.
In our case we want to compare BigDecimals so we can pass in a method reference which invokes BigDecimal's String constructor. This is the same as saying:
Since we converted from String to BigDecimal using this constructor the comparator will now use BigDecimal's compareTo method to sort the Array.