HelloKoding

Practical coding guides

Sort a string alphabetically in Java

Java does not have a direct method to sort a string. You can do it by converting the string to char array and use methods of Arrays class in java.util package to sort

Sort a String in alphabetically / ascending order

  • Convert a string str to char array by using str.toCharArray() method
  • Sort the char array ascending by using Arrays.sort method
  • Convert char array to the sorted string by using String.valueOf method

StringSortAscending.java

package com.hellokoding.java.collections;

import java.util.Arrays;

public class StringSortAscending {
    static String sortAsc(String str) {
        // convert String to char array
        char[] arr = str.toCharArray();

        // sort char array in alphabetically / ascending order
        Arrays.sort(arr);

        // String join char array
        return String.valueOf(arr);
    }

    public static void main(String[] args) {
        String str = "bac";
        System.out.println(sortAsc(str));
    }
}

Output

abc

Sort a String in descending order by using Java 8+ Stream API

  • Convert string str to Character[] array by using Stream API str.chars()
  • Sort the Character[] array descending by using Arrays.sort method with Comparator.reverseOrder
  • Convert Character[] array to the sorted string by using Arrays.stream method

StringSortDescending.java

package com.hellokoding.java.collections;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Collectors;

public class StringSortDescending {
    static String sortDesc(String str) {
        // convert String to Character array
        Character[] arr = str.chars().mapToObj(c -> (char)c).toArray(Character[]::new);

        // sort Character array in descending order
        Arrays.sort(arr, Comparator.reverseOrder());

        // String join Character array
        return Arrays.stream(arr).map(Objects::toString).collect(Collectors.joining());
    }

    public static void main(String[] args) {
        String str = "bac";
        System.out.println(sortDesc(str));
    }
}

Output

cba

Sort a String in descending order by using StringBuilder

  • Sort a string in ascending order by using Arrays.sort method
  • Then using reverse method of StringBuider to get the string in descending order

StringSortDescendingByStringBuilder.java

package com.hellokoding.java.collections;

import java.util.Arrays;

public class StringSortDescendingByStringBuilder {
    static String sortAsc(String str) {
        // convert String to char array
        char[] arr = str.toCharArray();

        // sort char array in alphabetically / ascending order
        Arrays.sort(arr);

        // String join char array
        return String.valueOf(arr);
    }

    static String sortDesc(String str) {
        return new StringBuilder(sortAsc(str)).reverse().toString();
    }

    public static void main(String[] args) {
        String str = "bac";
        System.out.println(sortDesc(str));
    }
}

Output

cba
Follow HelloKoding