Table of content

How to sort a string in Java

A string can be sorted alphabetically in ascending or descending order. Few methods to sort strings are discussed below.

Sorting a string in ascending order

Sorting a String using for loop

Strings can be sorted using for loop. To sort the string, the string is first converted to a character array using toCharArray() method. Then the array is iterated using for loop and it is sorted in ascending order.

import java.util.Arrays;
public class Test {
  public static void main(String[] args) {
    String str = "Semi Conductor";
    System.out.println("Before Sorting: " + str);
    char[] arry = str.toCharArray();
    for (int i = 0; i < arry.length; i++) {
      for (int j = i + 1; j < arry.length; j++) {
        // if all characters in string is lower case or uppercase
        // you can simply use - if(arry[j]<arry[i])
        if (Character.toUpperCase(arry[j]) < Character.toUpperCase(arry[i])) {
          char temp = arry[i];
          arry[i] = arry[j];
          arry[j] = temp;
        }
      }
    }
    System.out.println("After Sorting:" + String.valueOf(arry));
  }
}

The output is

Before Sorting: Semi Conductor
After Sorting: CcdeimnoorStu
Sorting a String using Arrays.sort() method

The Arrays.sort() method is a built-in method of the java.util.Arrays class. This method sorts the string in ascending order.

In this method, first, the string is converted to a character array and then the Arrays.sort() method is called.

import java.util.Arrays;
public class Example {
  public static void main(String[] args) {
    String input = "multidimensional";
    System.out.println("Before Sorting: " + input);
    char[] output = input.toCharArray();
    Arrays.sort(output);
    System.out.println("After Sorting: " + String.valueOf(output));
  }
}

The output is

Before Sorting: multidimensional
After Sorting: adeiiillmmnnostu
Sorting a String using Java 8 stream

The string can be sorted using Java 8 streams. The inbuilt method sorted() of Java streams sorts the string in alphabetically ascending order.

import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example {
  public static void main(String[] args) {
    String str = "black bull";
    System.out.println("Before sorting: " + str);
    String str1 = Stream.of(str.split("")).sorted().collect(Collectors.joining());
    System.out.println("After sorting: " + str1);
  }
}

The output is

Before sorting: black bull
After sorting:  abbcklllu

Sorting a string in descending order

Sorting a String using for loop

A string can be sorted in alphabetically descending order using for loop. First, the string has to be converted to a character array, then using two for loops each character is iterated and is sorted in descending order.

import java.util.Arrays;
public class Demo {
  public static void main(String[] args) {
    String str = "THE BLUE SKY";
    System.out.println("Before Sorting: " + str);
    char[] arry = str.toCharArray();
    for (int i = 0; i < arry.length; i++) {
      for (int j = i + 1; j < arry.length; j++) {
        // if all the characters in the string is not purely upper or lowercase
        // then use - if (Character.toUpperCase(arry[j]) > Character.toUpperCase(arry[i]))
        if (arry[j]>arry[i]) {
          char temp = arry[i];
          arry[i] = arry[j];
          arry[j] = temp;
        }
      }
    }
    System.out.println("After Sorting: " + String.valueOf(arry));
  }
}

The output is

Before Sorting: THE BLUE SKY
After Sorting: YUTSLKHEEB 
Sorting a String using the Collections.reverseOrder() method

The Collections class in Java has a reverseOrder() method that sorts the given input in descending order.

Therefore to sort a string in reverse order using this method, we will first convert the string to Arraylist and then the list is sorted.

import java.util.*;
public class Example {
  public static void main(String args[]) {
    String input = "Professional";
    System.out.println("Before sorting: " + input);
    List < Character > al = new ArrayList < > ();
    // converting the string to list  
    for (char ch: input.toCharArray()) {
      al.add(ch);
    }
    //sorts list in descending order  
    Collections.sort(al, Collections.reverseOrder());
    // without converting use - System.out.println(al);
    // converting the list to string
    StringBuilder output = new StringBuilder();
    for (Character str: al) {
      output.append(str);
    }
    System.out.println("After sorting: " + output);
  }
}

The output is

Before sorting: professional
After sorting: ssrpoonlifea
Sorting a String using Java 8 stream

The Java 8 stream can be used to sort a string in descending order. The Comparator.reverseOrder() can be used in the sorted() method of the Java streams.

import java.util.stream.*;
import java.util.*;
public class Example {
  public static void main(String[] args) {
    String str = "champion";
    System.out.println("Before sorting: " + str);
    String str1 = Stream.of(str.split("")).sorted(Comparator.reverseOrder())
                         .collect(Collectors.joining());
    System.out.println("After sorting: " + str1);
  }
}

The output is

Before sorting: champion
After sorting: ponmihca
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions