Java Collection How to - Sort a list








Question

We would like to know how to sort a list.

Answer

/*ww  w. j a  v  a  2 s .c  om*/


        
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class NameSort {
  public static void main(String args[]) {
    String n[] = new String[] { "John", "Lennon", "Karl", "Marx",
        "Groucho", "Marx", "Oscar", "Grouch" };

    List l = Arrays.asList(n);
    Collections.sort(l);
    System.out.println(l);
  }
}

The code above generates the following result.