Sort a Java Integer in Descending Order

The challenge

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.

Examples:

Input: 42145 Output: 54421

Input: 145263 Output: 654321

Input: 123456789 Output: 987654321

Test cases

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class DescendingOrderTest {

    @Test
    public void test_01() {
        assertEquals(0, DescendingOrder.sortDesc(0));
    }    

    @Test
    public void test_02() {
        assertEquals(51, DescendingOrder.sortDesc(15));
    }
    
    @Test
    public void test_03() {
        assertEquals(987654321, DescendingOrder.sortDesc(123456789));
    }
}

The solution in Java

public class DescendingOrder {
  public static int sortDesc(final int num) {
    
    // return if a negative number
    if (num<0) return num;
    
    // our number we will return
    int sortedNumber = 0;
    
    // loop with a maximum number int
    // perform loop in reverse
    for (int i = 9; i >= 0; i--) {
        // set a local number variable
        int tmpNumber = num;
        // while greater than 0
        while (tmpNumber > 0) {
            // get the digit
            int digit = tmpNumber % 10;
            // check for the greatest digit in the given number
            if (digit == i) {
                // build the sortedNumber value
                sortedNumber *= 10;
                sortedNumber += digit;
            }
            // reduce the loop variable
            tmpNumber /= 10;
        }
    }
    // return our sorted number
    return sortedNumber;
  }
}
Tags:
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments