Java Program to sort the given string Alphabetically using Comparator
Program
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
public class SortStringsUsingComparator {
public String sortStrings(String string)
{
Character tempArray[] = new Character[string.length()];
for (int i = 0; i < string.length(); i++) {
tempArray[i] = string.charAt(i);
}
Arrays.sort(tempArray, new Comparator<Character>(){
@Override
public int compare(Character c1, Character c2)
{
return Character.compare(c1, c2);
}
});
//String str = new String(tempArray);
// return str;
// using StringBuilder to convert Character array to String
StringBuilder sb = new StringBuilder(tempArray.length);
for (Character c : tempArray)
sb.append(c.charValue());
return sb.toString();
}
public static void main(String[] args){
SortStringsUsingComparator sortStringsUsingComparator = new SortStringsUsingComparator();
Scanner reader = new Scanner(System.in);
System.out.print("Enter a string to sort: ");
String enteredString = reader.nextLine();
String sortedString = sortStringsUsingComparator.sortStrings(enteredString);
System.out.println("Sorted String is "+ sortedString);
}
}
This program sorts the characters of a given string alphabetically using a custom Comparator.
-
Input:
- The program takes a string from the user.
-
Logic:
- Converts the string into an array of Character objects.
- Sorts the array using
Arrays.sort()
with a custom Comparator that compares characters alphabetically. - Converts the sorted Character array back into a string using a
StringBuilder
.
-
Steps:
- Convert the input string into a Character array.
- Use
Arrays.sort()
with a custom Comparator to sort characters alphabetically:- The comparator uses
Character.compare(c1, c2)
to compare two characters.
- The comparator uses
- Convert the sorted Character array into a string using a
StringBuilder
.
-
Output:
- The sorted string is printed to the user.
-
Key Features:
- Custom Sorting:
- The comparator allows customization if needed, such as sorting in reverse order or applying specific rules.
- Flexible Conversion:
- Converts from a string to a Character array and back efficiently using
StringBuilder
.
- Converts from a string to a Character array and back efficiently using
- Custom Sorting:
-
Example Execution:
- Input:
Enter a string to sort: banana
- Process:
- Convert "banana" to
['b', 'a', 'n', 'a', 'n', 'a']
(Character array). - Sort the array alphabetically using the comparator:
['a', 'a', 'a', 'b', 'n', 'n']
. - Convert back to string: "aaabnn".
- Convert "banana" to
- Output:
Sorted String is aaabnn
- Input:
-
Complexity:
- Time Complexity: O(n log n) due to sorting.
- Space Complexity: O(n) for the temporary character array and string.
-
Use Case:
- This program can sort strings with customizable sorting rules, making it useful for tasks like case-insensitive sorting or ordering based on specific criteria.
Comparison with Arrays.sort()
(Default):
- The default
Arrays.sort()
sorts primitive types or strings directly, whereas this program demonstrates custom sorting logic using Comparator.
Output
$ javac SortStringsUsingComparator.java
$ java SortStringsUsingComparator
Enter a string to sort: oodlescoop
Sorted String is cdeloooops