How do I sort the digits of a number in order? I want a four digit number to be changed around and made in order from smallest to largest/largest to smallest.
For example, given 8493, I want that number to become 3489.
How do I sort the digits of a number in order? I want a four digit number to be changed around and made in order from smallest to largest/largest to smallest.
For example, given 8493, I want that number to become 3489.
Hack the number into digits, sort them and put them back together.
In your case: 8493 -> {8, 4, 9, 3} -> {3, 4, 8, 9} -> 3489
With 2 digits: 51 -> {5, 1} -> {1, 5} -> 15
With 12 digits: 511111011111 -> {5, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1} -> {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5} -> 11111111115 (here it depends if you want to keep the 0 or not.
Convert int to string, then use Best way to reverse a string. If needed convert string back to int.