java sort string characters alphabetically

Let’s learn java sort string characters alphabetically.

java sort string characters alphabetically

Here we are going to sort string characters alphabetically using sort() method of Arrays class.

Here’s the java code to arrange the letters of a string in alphabetical order.

import java.util.Arrays;
public class CharactersAlphabetically
{
   public static void main(String[] args)
   {
      String strInput = "flowerbrackets";
      // converting string to char array
      char[] ch = strInput.toCharArray();
      // sorting char array
      Arrays.sort(ch);
      // converting char array to string
      String strSorted = String.valueOf(ch);
      System.out.println("sort string characters alphabetically: " + strSorted);
   }
}


Output:

sort string characters alphabetically: abceefklorrstw


Also read – polymorphism in java