how to order alphabetically java code example

Example 1: java sort string characters alphabetically

// java sort string characters alphabetically
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);
   }
}

Example 2: java sort list alphabetically

Assuming that those are Strings, use the convenient static method sort…

 java.util.Collections.sort(listOfCountryNames)

Tags:

Java Example