Sort a list

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]
Home 
  Java Book 
    Collection  

Collections:
  1. Collections
  2. Get empty collection from Collections
  3. Do binary search
  4. Copy value to a List
  5. Get Enumeration from collection, create list from Enumeration
  6. Fill a list
  7. Get the max and min value from a Collection
  8. Fill n Copy object to a list
  9. Replace value in a list
  10. Reverse a list
  11. Get comparator in reverse order
  12. Rotate and shuffle a list
  13. Create singleton
  14. Sort a list
  15. Swap element in a list
  16. Get a synchronized collection
  17. Return an unmodifiable view of collections