Java Coding Test Course, Sorting Digits in Descending Order

Problem Description

Unlike typical sorting problems, this problem requires you to sort each digit of the given number in descending order and output the newly formed number.
It is assumed that the number input from the user is a non-negative integer, and each digit consists of numbers from 0 to 9. For example, if 321 is input,
it is sorted in descending order and 321 is output as is, and if 2143 is input, 4321 is output.

Input Format and Conditions

  • The input is a single integer that includes each digit.
  • The input value must be within the range of a 32-bit integer, which is greater than or equal to 0.
  • If the input value is 0, 0 should be output.

Example Test Cases

Input Output
2143 4321
321 321
1110 1110
0 0

Approach to the Problem

The approach to solving the problem is as follows.
1. **Input Value String Processing**: First, convert the input integer to a string to separate each digit.
2. **Digit Array Formation**: Store each digit of the string-converted number into an array.
3. **Sorting**: Sort each digit in the array in descending order.
4. **Output Format**: Concatenate the sorted digits into one string and output it.

Detailed Algorithm Process

Let’s take a closer look at the algorithm step by step.

Step 1: Input Processing

Convert the number input by the user to a string.
The value entered by the user can be retrieved through Java’s Scanner class.
For example:

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

Step 2: Array of Digits

To construct an array of each character in the input string, you can use the toCharArray() method of the String class.
This method converts each character that makes up the string into a character array.

char[] digits = input.toCharArray();

Step 3: Sorting

You can use the Arrays.sort method for sorting, but to sort in descending order,
a Comparator method is needed. The code is as follows:

Arrays.sort(digits, Collections.reverseOrder());

Step 4: Output Format

Convert the sorted character array back into a string and output the final result.
You can process it with the following code:

String result = new String(digits);
System.out.println(result);

Final Code Implementation

The final code that integrates all steps is as follows:

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class DescendingOrder {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        String input = scanner.nextLine();

        if (input.equals("0")) {
            System.out.println("0");
            return;
        }

        char[] digits = input.toCharArray();
        Arrays.sort(digits, Collections.reverseOrder());
        
        String result = new String(digits);
        System.out.println(result);
    }
}

Code Explanation and Additional Optimization

The above code meets the basic requirements. However, some parts of the code can be further optimized.
For example, **negative number handling** and clear **input validation** can be added. It may be necessary to check
whether the string received from the user is actually a number.

if (!input.matches("\\d+")) {
            System.out.println("Invalid input.");
            return;
        }

Testing and Validation

Once the final code is complete, it needs to be tested with various inputs to check if it works properly.
You should try values such as 0, negative numbers, and numbers of various lengths.
The program can be validated with test cases such as:

  • Input: 98765 → Output: 98765
  • Input: 10203 → Output: 32100
  • Input: 9001 → Output: 9100

Conclusion

In this tutorial, we have explained in detail how to sort the digits of a given number in descending order using Java.
We hope you understand the process of solving the problem through a step-by-step approach with the final code.
In real practice, you may encounter more complex problems, so it is important to improve your problem-solving abilities
through regular practice. Thank you.