Skip to content

Commit

Permalink
BigDecimla problem
Browse files Browse the repository at this point in the history
  • Loading branch information
javasks committed Mar 16, 2020
1 parent 63e7add commit 0bb71d5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/com/sks/hkr/java/bigNumber/BigDecimalProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,27 @@ public static void main(String[] args) {
}
sc.close();

// We want to sort in descending order while preserving the contents
// of each String. A comparator can achieve this for us. We convert
// to BigDecimal inside our comparator so that the change is not
// permanent and our String's form for each number is preserved.
Comparator<String> customComparator = new Comparator<String>() {

/**
* We want to sort in descending order while preserving the contents
* of each String. A comparator can achieve this for us. We convert
* to BigDecimal inside our comparator so that the change is not
* permanent and our String's form for each number is preserved.
*/
/*Comparator<String> customComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
BigDecimal a = new BigDecimal(s1);
BigDecimal b = new BigDecimal(s2);
return b.compareTo(a); // descending order
}
};*/

/**
* Using Lambda
*/
Comparator<String> customComparator = (s1, s2) -> {
return new BigDecimal(s2).compareTo(new BigDecimal(s1));
};

Arrays.sort(s, 0, n, customComparator);
Expand Down

0 comments on commit 0bb71d5

Please sign in to comment.