Sort a String in Java

Sort String : Sorting of String can be done in two ways – Default Sorting in Java and Custom Sorting in Java

Sort String : Default Sorting in Java

Default Sorting is done in java using the APIs provided by java. It does on the basis of the ASCII value of characters of a String.

Sort String : Custom Sorting in Java:

Custom Sorting is done in java using Comparator class in java. We override the compare() method in Comparator class and pass comparator object as an argument in the APIs provided by Java.

Demo Program for Default Sorting in Java:

The below examples illustrates as to how to do default sorting of String in Java. Default Sorting uses ASCII value of characters to sort it.

// demo program to sort string
package com.mathapachi;
import java.util.Arrays;
public class SortStringDemoProgram {

	public static void main(String[] args) {
         String str = "jhfdwheudhoqmxmsbjfdvj";
         //for converting into character array
         char[] charArray = str.toCharArray(); 
         
         //for sorting character array
         Arrays.sort(charArray);
         
         //passing character array as in string constructor to create new String
         //as string is immutable
         String sortedStr = new String(charArray);
         System.out.println("Original String = "+str); 
         System.out.println("Sorted String = "+sortedStr); 
	}

}

Output of the Above Program is as below:

Original String = jhfdwheudhoqmxmsbjfdvj
Sorted String = bdddeffhhhjjjmmoqsuvwx

What happens in Default Sorting when String contains a mix of uppercase and lowercase characters:

When the string contains a mix of uppercase and lowercase letters , then default sorting gives a little unexpected result than what we expect. It is because of the fact that the default sorting uses ASCII value of characters for sorting.
Below demo program illustrates as to how it behaves in default sorting when the string contains mix of uppercase and lowercase letters.

package com.mathapachi;
import java.util.Arrays;
public class SortStringDemoProgram {

	public static void main(String[] args) {
         String str = "AaDcC";
         //for converting into character array
         char[] charArray = str.toCharArray(); 
         
         //for sorting character array
         Arrays.sort(charArray);
         
         //passing character array as in string constructor to create new String
         //as string is immutable
         String sortedStr = new String(charArray);
         System.out.println("Original String = "+str); 
         System.out.println("Sorted String = "+sortedStr); 
	}

}

Output of the above program is as below:

Original String = AaDcC
Sorted String = ACDac

What we could have expected is that character ‘a’ and ‘A’ should have been side by side but it is not. It is because of the fact that the ASCII value of lowercase characters is different from Uppercase characters.

To overcome this shortcoming in Default Sorting , we should go for Custom Sorting of String in Java.

Demo Program for Custom Sorting in Java:

Custom sorting can be done with the help of comparator class. The below demo program illustrates custom sorting of string:

//demo program to sort string using custom comparator
package com.mathapachi;
import java.util.Comparator;
public class MyComparator implements Comparator{ 
	@Override
	public int compare(Character ch1, Character ch2) {
		Character lowerCase1 = Character.toLowerCase(ch1); 
		Character lowerCase2 = Character.toLowerCase(ch2);
		return lowerCase1.compareTo(lowerCase2); 
	}

}
----------------------------------------------------

package com.mathapachi;
import java.util.Arrays;
public class SortStringDemoProgram {

	public static void main(String[] args) {
         String str = "MAThapAchiSoftWarEWebsite";
        
         Character tmpArr[] = new Character[str.length()];
         for(int i=0;i<str.length();i++) {
        	 tmpArr[i] = str.charAt(i);
         }
         
         //for sorting character array
         Arrays.sort(tmpArr,new MyComparator());
         
         //for converting into character array
         char[] charArray = new char[str.length()];
         for(int i=0;i<str.length();i++) {
        	 charArray[i] = tmpArr[i];
         }
         
         String sortedStr = new String(charArray);
         System.out.println("Original String = "+str); 
         System.out.println("Sorted String = "+sortedStr); 
	}

}

Output of the above program of custom sorting of String is as below:

Original String = MAThapAchiSoftWarEWebsite
Sorted String = AaAabcEeefhhiiMoprSsTttWW