Sort String Programmatically in Java

Program 1 : Java Program to sort a string without using inbuilt sort() method

public class StringSortProgram1 {

//  Java Program to sort a string without using inbuilt sort() method	

    void merge(char arr[], int left, int mid, int right)
    {
        
        int n1 = mid - left + 1;
        int n2 = right - mid;

        char L[] = new char[n1];
        char R[] = new char[n2];

        for (int i = 0; i < n1; ++i)
            L[i] = arr[left + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[mid + 1 + j];

        int i = 0, j = 0;
        int k = left;
        
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            }
            else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    void mergeSort(char arr[], int start, int end)
    {
        if (start < end) {
            int mid = start + (end - start) / 2;
          
            mergeSort(arr, start, mid);
            mergeSort(arr, mid + 1, end);          
            merge(arr, start, mid, end);
        }
    }

    public static void main(String args[])
    {
        String inputString = "ksamyatam";
        char arr[] = inputString.toCharArray();
        

        System.out.println("Original String array : ");
        for (char val : arr) {
            System.out.print(val);  
        }

        StringSortProgram1 ob = new StringSortProgram1();
        ob.mergeSort(arr, 0, arr.length - 1);

        System.out.println("\n\nSorted String array : ");
        for (char val : arr) {
            System.out.print(val);  
        }
    }
}
Output :

Original String array : 
ksamyatam

Sorted String array : 
aaakmmsty

Program 2 : Java Program to sort a string without using sort() method

import java.util.Arrays;

public class StringSortProgram2 {

//  Java Program to sort a string without using sort() method	

	public static String sortString(String str)
    {
        char charArr[] = str.toCharArray();

        Arrays.sort(charArr);
		
		String str_result = new String(charArr);

        return str_result;
    }

    public static void main(String[] args)
    {
        String strOriginal = "ksamyatam";
        String strResult = sortString(strOriginal);

        System.out.println("Original String : " + strOriginal);
        System.out.println("Sorted String : " + strResult);
    }
}
Output :

Original String : ksamyatam
Sorted String : aaakmmsty

Program 3 : Java Program to sort a string without using Arrays.sort(T [ ], Comparator c) method

import java.util.Arrays;
import java.util.Comparator;

public class StringSortProgram3 {

//  Java Program to sort a string without using  Arrays.sort(T [ ], Comparator c) method	

	public static String sortString(String str)
    {
        Character tempArr[] = new Character[str.length()];

        for (int i = 0; i < str.length(); i++) {
            tempArr[i] = str.charAt(i);
        }

        Arrays.sort(tempArr, new Comparator<Character>() {
            
            @Override
            public int compare(Character c1, Character c2)
            {
                return Character.compare(Character.toLowerCase(c1),Character.toLowerCase(c2));
            }
        });

        StringBuilder sb = new StringBuilder(tempArr.length);

        for (Character c : tempArr)
            sb.append(c.charValue());

        return sb.toString();
    }

    public static void main(String[] args)
    {
        String strOriginal = "ksamyatam";
        String strResult = sortString(strOriginal);

        System.out.println("Original String : " + strOriginal);
        System.out.println("Sorted String : " + strResult);
    }
}
Output :

Original String : ksamyatam
Sorted String : aaakmmsty
Scroll to Top