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]
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course