Sort String in Ascending Order in Java

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

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

    private static void sortStringsInAscOrder() {
        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);
        System.out.println("After Sorting in Asc Order: " + names);
    }
}

Output

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


Comments