Java Program to sort the given string in reverse order using Comparator


import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
public class SortStringsInReverseUsingComparator {
    public String sortStringsReverse(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>(){
            public int compare(Character c1, Character c2)
                return, c1);
        StringBuilder sb = new StringBuilder(tempArray.length);
        for (Character c : tempArray)
        return sb.toString();
    public static void main(String[] args){
        SortStringsInReverseUsingComparator sortStringsInReverseUsingComparator = new SortStringsInReverseUsingComparator();
        Scanner reader = new Scanner(;
        System.out.print("Enter a string to sort: ");
        String enteredString = reader.nextLine();
        String sortedString = sortStringsInReverseUsingComparator.sortStringsReverse(enteredString);
        System.out.println("Sorted String is "+ sortedString);                


$ javac
$ java SortStringsInReverseUsingComparator
Enter a string to sort: oodlescoop
Sorted String is spooooledc