java.util.Collections.sort(myList);
This tutorial shows how to sort a list using java with the Collection library.
First, a List of type ArrayList is created. Values in a random order are added to the list. Then the sort method from the collections class is called.
Finally, the content of the list is shown in the output.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListSort { public static void main(String[] argv) { // Define the list List<String> myList = new ArrayList(); // Add the values myList.add("San Francisco"); myList.add("New York"); myList.add("Paris"); myList.add("London"); // Sort the list Collections.sort(myList); // Loop on the list for (String current : myList) { // Write the element in the output System.out.println(current); } } }
London New York Paris San Francisco