Java Program to Sort String in Ascending Order

sorting string in ascending order in java

In this tutorial, we will learn the java program to sort all the characters of the string in ascending order.

For example

Case1: If the user inputs the string ‘python’

Then the output should be ‘honpty’, where all the characters are sorted.

Case2: If the user inputs the string ‘quescol’

Then the output should be ‘celoqsu’, where all characters are sorted.

Java Program to Sort String in Ascending Order

Output

Java Program to Sort String in Ascending Order

Explanation

For the input string ‘quescol’, firstly, the string’s elements get stored in a character array that looks like

                    {‘q’, ‘u’, ‘e’, ‘s’, ‘c’, ‘o’, ‘l’ }

Then, after using the sorting and re-arranging the array elements in ascending order as

          [ ‘c’, ‘e’, ‘l’, ‘o’, ‘q’, ‘s’, ‘u’ ]

Finally, concatenating and returning the final sorted string as output is “celoqsu”.