Sort a list

ReturnMethodSummary
static<T extends Comparable<? super T>>void sort(List<T> list) Sorts the specified list into ascending order, according to the natural ordering of its elements.
static<T> void sort(List<T> list, Comparator<? super T> c) Sorts the specified list according to the order induced by the specified comparator.

  import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class Main {
  public static void main(String args[]) {
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    System.out.print("List sorted in reverse: ");
    System.out.print(ll);
  }
}

The output:


List sorted in reverse: [20, 8, -8, -20]

Sort elements of ArrayList


import java.util.ArrayList;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("java2s.com");
    Collections.sort(arrayList);

    System.out.println(arrayList);
  }
}

The output:


[1, 2, 3, 4, java2s.com]

Sort elements of Vector


import java.util.Vector;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("3");
    v.add("5");
    v.add("2");
    v.add("java2s .com ");
    Collections.sort(v);

    System.out.println(v);
  }
}

The output:


[1, 2, 3, 5, java2s .com ]

Sort ArrayList in descending order using comparator


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("java2s.com");
    Comparator comparator = Collections.reverseOrder();
    System.out.println("Before: " + arrayList);
    Collections.sort(arrayList, comparator);
    System.out.println("After sorting: " + arrayList);
  }
}

The output:


Before: [A, B, C, D, java2s.com]
After sorting: [java2s.com, D, C, B, A]

Sort Vector in descending order using comparator


import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("j ava2s.com");
    Comparator comparator = Collections.reverseOrder();
    System.out.println("Before sorting: " + v);
    Collections.sort(v, comparator);
    System.out.println("After sorting: " + v);
  }
}

The output:


Before sorting: [1, 2, 3, 4, j ava2s.com]
After sorting: [j ava2s.com, 4, 3, 2, 1]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.