Skip to content
Advertisement

Big Decimal array not sorting 0 and 000.000

I was solving a java HackerRank problem in which I had to sort an array consisting decimal numbers using BigDecimal class in descending order. The solution works fine except for one case in which 0 and 000.000 comes. Now as they are equal, the problem tells us not keep them in the same order of their occurrence, but it is not happening.

My Code:

JavaScript

Sample Input : 9 —> the size of array

  1. -100
  2. 50
  3. 0
  4. 56.6
  5. 90
  6. 0.12
  7. .12
  8. 02.34
  9. 000.000

Advertisement

Answer

You problem is stability of sort. You should select a stable sort algorithm. Insertion sort is such one.

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement