Example usage for java.util Collections reverseOrder

List of usage examples for java.util Collections reverseOrder

Introduction

In this page you can find the example usage for java.util Collections reverseOrder.

Prototype

@SuppressWarnings("unchecked")
public static <T> Comparator<T> reverseOrder() 

Source Link

Document

Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

Usage

From source file:Main.java

public static void main(String[] args) {
    Integer[] arrayToSort = new Integer[] { new Integer(5), new Integer(89), new Integer(16), new Integer(2) };

    Arrays.sort(arrayToSort, Collections.reverseOrder());

    for (Integer i : arrayToSort) {
        System.out.println(i.intValue());
    }/*w w w . j a  va2 s  .c  o  m*/
}

From source file:MainClass.java

public static void main(String[] a) {
    List list = new ArrayList();
    list.add("A");
    list.add("C");
    list.add("B");
    Collections.sort(list, Collections.reverseOrder());
    System.out.println(list);//  ww w .j  a  va 2 s . c  o  m
}

From source file:AlgorithmsDemo.java

public static void main(String args[]) {

    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//from   ww w .j  ava  2  s  .  c  o  m
    ll.add(20);
    ll.add(-20);
    ll.add(8);

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

    Collections.sort(ll, r);

    for (int i : ll)
        System.out.print(i + " ");

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();

    v.add("1");//from  w  w w  . j a  v  a2 s . c  o  m
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");

    Comparator comparator = Collections.reverseOrder();
    System.out.println(v);

    Collections.sort(v, comparator);
    System.out.println(v);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "S", "P", "E", "G", "P" };
    Set set = new TreeSet(Collections.reverseOrder());
    for (int i = 0, n = elements.length; i < n; i++) {
        set.add(elements[i]);/*from  ww w. j ava 2  s .  c  om*/
    }
    System.out.println(set);
    System.out.println(((TreeSet) set).comparator());
}

From source file:MainClass.java

public static void main(String args[]) {
    ArrayList simpsons = new ArrayList();
    simpsons.add("Bart");
    simpsons.add("Hugo");
    simpsons.add("Lisa");
    simpsons.add("Marge");
    simpsons.add("Homer");
    simpsons.add("Maggie");
    simpsons.add("Roy");
    Comparator comp = Collections.reverseOrder();
    Collections.sort(simpsons, comp);
    System.out.println(simpsons);
}

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted short array
    Short[] sArr = new Short[] { 3, 13, 1, 9, 21 };

    System.out.println(Arrays.toString(sArr));

    // create a comparator
    Comparator<Short> comp = Collections.reverseOrder();

    // sorting array with reverse order using comparator
    Arrays.sort(sArr, comp);/*from ww w  .  j  a va 2 s.c o  m*/

    System.out.println(Arrays.toString(sArr));
}

From source file:Main.java

public static void main(String[] args) {

    // initializing unsorted short array
    Short sArr[] = new Short[] { 3, 13, 1, 9, 21 };

    System.out.println(Arrays.toString(sArr));

    // create a comparator
    Comparator<Short> comp = Collections.reverseOrder();

    // sorting array with reverse order using comparator from 0 to 2
    Arrays.sort(sArr, 0, 2, comp);

    System.out.println(Arrays.toString(sArr));
}

From source file:Main.java

public static void main(String args[]) {
    String str[] = { "B", "H", "L", "M", "R" };
    List list = Arrays.asList(str);
    System.out.println(Collections.min(list));
    System.out.println(Collections.max(list));
    Comparator comp = Collections.reverseOrder();
    System.out.println(Collections.min(list, comp));
    System.out.println(Collections.max(list, comp));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] strArray = new String[] { "z", "a", "A" };
    List list = Arrays.asList(strArray);
    Collections.sort(list);/*from  w w  w.  j a v a2  s .co  m*/

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

    Collections.sort(list, Collections.reverseOrder());

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    Collections.reverse(list);
}