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.
I loved this problem! Took me forever to figure it out.
All you need is a comparator. Why? Because you need to keep the strings in the array, but still compare the strings by their BigDecimal values. This is achieved through the comparator.
Converting the strings to BigDecimals and storing the BigDecimal values will lead to the problem of losing leading zeros on certain string values, so you need the comparator.
MyCode{
Arrays.sort(s, Collections.reverseOrder(new Comparator<String>() {
@Override
public int compare(String a1, String a2) {
//convert to big decimal inside comparator
//so permanent string values are never changed
//aka you only use the BigDecimal values to
//compare the strings!
BigDecimal a = new BigDecimal(a1);
BigDecimal b = new BigDecimal(a2);
return a.compareTo(b);
}
}));
Java BigDecimal
You are viewing a single comment's thread. Return to all comments →
I loved this problem! Took me forever to figure it out.
All you need is a comparator. Why? Because you need to keep the strings in the array, but still compare the strings by their BigDecimal values. This is achieved through the comparator.
Converting the strings to BigDecimals and storing the BigDecimal values will lead to the problem of losing leading zeros on certain string values, so you need the comparator.
MyCode{
}