Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new String with sorted letters from a String word in Java

How do I create a String with alphabetical order letters taken from another String?

Let's say I have something like this

String theWord = "Hello World";

How do I compute the new String to make it look like"

dehllloorw

Which is theWord but sorted character by character in alphabetical order.

Thanks in advance

like image 342
randomizertech Avatar asked Jun 07 '11 14:06

randomizertech


1 Answers

char[] chars = theWord.toCharArray();
Arrays.sort(chars);
String newWord = new String(chars);
like image 98
Sean Patrick Floyd Avatar answered Sep 18 '22 15:09

Sean Patrick Floyd