Monday, August 8, 2011

Java: Sort the content of a string


Share at Facebook

Arrays of java.util is very useful for sorting and other stuffs. You can easily sort your String using the sort() function of that class. Below is the sample code.


String string = "String to sort";
char[] array = string.toCharArray();
java.util.Arrays.sort(array);
string = new String(array);
System.out.println(string);


Basically this simple piece of code is converting the string into a character array, and then sorting it and return back to the string again. Simple...!




No comments: