Java Sort String Characters Alphabetically

Java example to sort characters of String in alphabetical order – using Stream.sorted() and Arrays.sort() methods.

1) Sort string with Stream API

Example of sorting the characters of string using Stream.sorted() API.

String randomString = "adcbgekhs";
String sortedChars = Stream.of( randomString.split("") )

System.out.println(sortedChars);	// abcdeghks

2) Arrays.sort()

Example of sort a string using Arrays.sort() method.

String randomString = "adcbgekhs";
//Convert string to char array
char[] chars = randomString.toCharArray();	

//Sort char array

//Convert char array to string
String sortedString = String.valueOf(chars);

System.out.println(sortedChars);	// abcdeghks

Drop me your questions in comments section.

Happy Learning !!


Stream.sorted() Java Doc
Arrays.sort() Java Doc

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Java Sort String Characters Alphabetically”

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.