Sort String in Descending Order in Java

In this example, we will sort the String objects in descending order using Collections.sort(names,Collections.reverseOrder()) method.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortStringsExample {
    public static void main(String[] args) {
         sortStringsInDescOrder();
    }

    private static void sortStringsInDescOrder() {
        List<String> names = new ArrayList<>();
        names.add("ABC");
        names.add("ACB");
        names.add("PQR");
        names.add("CDQ");

        System.out.println("Before Sorting : " + names);
        Collections.sort(names,Collections.reverseOrder());
        System.out.println("After Sorting in Desc Order : " + names);
    }
}

Output

Before Sorting : [ABC, ACB, PQR, CDQ]
After Sorting in Desc Order : [PQR, CDQ, ACB, ABC]

Comments