To sort a
String
in Java, first get characters with toCharArray
, then sort them with Arrays.sort
, finally make a new String
from them.Here's how you do it:
String string = "Hello World!";
char[] characters = string.toCharArray();
java.util.Arrays.sort(characters);
String sortedString = new String(characters);
System.out.println(sortedString); // !HWdellloor