How to Sort a String in Java

Abdul Mateen Feb 02, 2024
  1. Use Array.sort() Method to Sort a Single String in Java
  2. Use the for Loop to Sort a Single String in Java
How to Sort a String in Java

This tutorial article demonstrates how to sort a single string in Java.

The most common order used is alphabetical order. Sorting is the process of converting data to a standard format and create a human-readable format.

There are two ways to sort a single string in Java as follows.

  • Array.sort() Method
  • User-Defined Method

Use Array.sort() Method to Sort a Single String in Java

A string is immutable in Java, which means the string cannot be changed in memory if a string object has been created once. We also can’t access a single character in a string.

We can use the Array.sort() method to sort a single string in Java. The string class has no sorting method. When sorting the string, the main idea is to convert the given string to an array of characters using the toCharArray() method. And then, we use the Arrays.sort() method to sort that array.

The example of sorting a single string using the Arrays.sort() method is as follows.

import java.util.Arrays;

public class StringSorting {
  public static void main(String args[]) {
    String STR = "java";
    char[] StringtoChar = STR.toCharArray();
    Arrays.sort(StringtoChar);
    String SortedString = new String(StringtoChar);
    System.out.println("The Unsorted String is : " + STR);
    System.out.println("The Sorted String is : " + SortedString);
  }
}

Output:

The Unsorted String is : java
The Sorted String is : aajv

In the above code, we create a string that stores java in it. After converting it into a character array using the toCharArray() method, we sort the converted character array using Arrays.sort, and typecast the sorted array to a string.

Use the for Loop to Sort a Single String in Java

We can sort a single array using the for loop and compare the element with the other in every iteration. But as we know, we cannot access a single character in a string to convert that string into a character array. Then we iterate the character array by using two loops to compare each element with the other element.

The example of sorting a single string using the User-Defined method is as follows.

import java.util.Arrays;

public class StringSorting {
  public static void main(String args[]) {
    String STR = "java";
    char[] StringtoChar = STR.toCharArray();
    for (int i = 0; i < (StringtoChar.length - 1); i++) {
      for (int j = i + 1; j > 0; j--) {
        if (StringtoChar[j] < StringtoChar[j - 1]) {
          char Temp = StringtoChar[j - 1];
          StringtoChar[j - 1] = StringtoChar[j];
          StringtoChar[j] = Temp;
        }
      }
    }
    String SortedString = new String(StringtoChar);
    System.out.println("The Unsorted String is : " + STR);
    System.out.println("The Sorted String is : " + SortedString);
  }
}

Output:

The Unsorted String is : java
The Sorted String is : aajv

In the above code, we create a string that stores java in it.

We use two for loops here. The first loop is to iterate the character array, and the second loop avoids the repetition in comparison. If the condition (StringtoChar[j]<StringtoChar[j-1]) is true inside the second loop, it performs the swapping and sorts the array.

Related Article - Java String