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

Oldest Most Voted
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.