方法一:
String strToSorted = new String("cdeafghibaefh");
char[] charArray = strToSorted.toCharArray();
Arrays.sort(charArray);
System.out.println(charArray);
方法二:
String strToSorted = "strToSorted";
List<Character> charList = asList(strToSorted.toCharArray());
Collections.sort(charList);
char[] chars = new char[charList.size()];
for (int i = 0; i < charList.size(); i++) {
chars[i] = charList.get(i);
}
System.out.println(chars);
方法三(计数法):
String strToSorted = "strToSorted";
char[] asciis = new char[256];
for (int i = 0; i < strToSorted.length(); i++) {
asciis[strToSorted.charAt(i)]++;
}
char[] chars=new char[strToSorted.length()];
int index=0;
for (int i = 0; i < 256; i++) {
if(asciis[i] !=0){
int count=asciis[i];
for (int j = 0; j < count; j++) {
chars[index++]= (char)i;
}
}
}
System.out.println(chars);