Java ProgramsStringsSort Characters in a String

Sort Characters in a String in Java

beginner·  Strings  ·  String Manipulation

Problem

Sorting a string's characters means rearranging every character it contains into ascending order, based on each character's code.

Given a string, arrange its characters in alphabetical order.

Input
banana
Output
aaabnn

Java Program

Java
import java.util.Arrays; public class SortCharacters { public static void main(String[] args) { String str = "banana"; char[] chars = str.toCharArray(); // copy the characters out into a mutable array Arrays.sort(chars); // sorts the array in place, ascending by character code System.out.println(new String(chars)); // rebuilds a String from the sorted array } }

Output

aaabnn

Core Logic

Converting the string into a character array, sorting that array in place, and rebuilding a string from it reorders every character.

How It Works
  1. 1str.toCharArray() converts the string into a char[] holding every character.
  2. 2Arrays.sort(chars) sorts that array in place, in ascending order by character code.
  3. 3new String(chars) builds a brand-new String from the now-sorted array.
For "banana", the array ['b','a','n','a','n','a'] sorts to ['a','a','a','b','n','n'], producing "aaabnn".
💡

Key Point: Arrays.sort() works directly on the char[], not the original String — strings are immutable in Java, so the characters have to be copied out into a mutable array first.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: toCharArray() copies every character into a new array, and Arrays.sort() then runs an O(n log n) comparison sort over it.

Key Concepts

toCharArray()Arrays.sort()String constructor

Approach 2: Java 8

Java
import java.util.stream.Collectors; public class SortCharactersStream { public static void main(String[] args) { String str = "banana"; // sorted() sorts the character codes; the rest reassembles them into a String String result = str.chars() .sorted() .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println(result); } }

Output

aaabnn

Core Logic

The same sort can be expressed as a stream pipeline — sort the character codes directly, then join them back into a string.

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.sorted() puts those codes into ascending order, the stream equivalent of Arrays.sort() on a char[].
  3. 3.mapToObj(c -> String.valueOf((char) c)) converts each sorted code back into a one-character String.
  4. 4.collect(Collectors.joining()) concatenates them all back into the final sorted string.
Sorting "banana"'s character codes and joining them produces the same result as the array version: "aaabnn".
💡

Key Point: This does the same O(n log n) sort as the array version, just without ever touching a char[] directly.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: sorted() still performs the same comparison sort over every character, and Collectors.joining() builds a result string holding all n characters.

Key Concepts

Streamchars()sorted()Collectors.joining()

Related Programs