Sorting a string in Java is something which is not in-built Java feature in which direct method is available within Java library to sort string though Java does provide techniques to sort array of elements by using Arrays.sort. Using few different methods which are provided by Java library we can sort in Java easily and sorting a string has various different uses cases in any Java application.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
It has 2 major techniques which can be used by developer depending on the use case.
This is a very simple and straight forward technique. The way we sort array of element in Java similar way we and similar logic will be applied.
To implement this technique below are the steps and an example to this sorting:
Example:
Code:
import java.util.Arrays; import java.util.Scanner; public class SortString { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Input String Value which should be sorted : "); String inputString = scanner.nextLine(); char arrayOfCharacters[] = inputString.toCharArray(); // Step 1 Arrays.sort(arrayOfCharacters); // Step 2 String sortedString = new String(arrayOfCharacters); // Step 3 System.out.println("Before Sorting : " + inputString); System.out.println("After Sorting : " + sortedString); } }
Output:
Limitations:
Example:
Output:
This technique is used mainly when the input String can be of mixed characters with lowercase as well as upper case. This is not a straight forward technique as above and requires some bit of more coding. We will be using the same array sorting technique provided by Java but along with that we will use comparator which will help us compare each characters in the string.
To implement this technique below are the steps and an example to this type of string sorting in Java.
Example:
Code:
import java.util.Arrays; import java.util.Scanner; import java.util.Comparator; public class SortString { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Input a String Value which should be sorted : "); String inputString = scanner.nextLine(); Character arrayOfCharacters[] = new Character[inputString.length()]; for (int i = 0; i < inputString.length(); i++) { arrayOfCharacters[i] = inputString.charAt(i); // Step 1 } Arrays.sort(arrayOfCharacters, new Comparator<Character>(){ // Step 2 @Override public int compare(Character c1, Character c2) { return Character.compare(Character.toLowerCase(c1), Character.toLowerCase(c2)); } }); StringBuilder outputString = new StringBuilder(arrayOfCharacters.length); for (Character c : arrayOfCharacters) outputString.append(c.charValue()); // Step 3 System.out.println("Before Sorting : " + inputString); System.out.println("After Sorting considering casing: " + outputString.toString()); } }
Output:
Limitations:
This technique is a new technique with minimal line of code and using Java 8 Stream features along with String.chars method which comes inline with Java 8 only. So make sure when you apply this technique your Java application used mainly when the input string can be of mixed characters with lowercase as well as upper case. This is not a straight forward technique as above and requires some bit of more coding. We will be using the same Array sorting technique provided by Java but along with that we will use comparator which will help us compare each characters in the string.
Sorting a String in Java is very easy and comes with lot of different approaches and developer can use any of these depending on the requirement of the application. It varies from 2 liner code to writing a whole new method to sort and also allows to sort a string by ignore the case as well.
The above is the detailed content of Sort String in Java. For more information, please follow other related articles on the PHP Chinese website!