Example usage for java.util Collections sort

List of usage examples for java.util Collections sort

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> void sort(List<T> list, Comparator<? super T> c) 

Source Link

Document

Sorts the specified list according to the order induced by the specified comparator.

Usage

From source file:Main.java

public static void main(String... args) {
    List<String> names = Arrays.asList("XML", "Java", "HTML", "CSS");
    Collections.sort(names, (String a, String b) -> b.compareTo(a));
    System.out.println(names);/*from w  w  w.  j a v a  2 s  .c om*/

    Collections.sort(names, (a, b) -> b.compareTo(a));
    System.out.println(names);

}

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);//from  ww w .java  2  s  .c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    List<Value> list = Arrays.asList(Value.values());
    System.out.println(list);/*from  w w  w.ja  v  a  2s .com*/

    Collections.sort(list, (Value o1, Value o2) -> o1.getValue1() - o2.getValue1());
    System.out.println(list);
    Collections.sort(list, (Value o1, Value o2) -> o1.getValue2() - o2.getValue2());
    System.out.println(list);
}

From source file:AlgorithmsDemo.java

public static void main(String args[]) {

    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//from   w w w. j av  a  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) {
    List<String> list = new ArrayList<>();
    list.add("acowa");
    list.add("cow");
    list.add("aow");
    Collections.sort(list, new Comparator<String>() {

        @Override/* w w w. j a  va 2 s. c o m*/
        public int compare(String o1, String o2) {
            if (o1.length() > o2.length()) {
                return 1;
            } else {
                return o1.compareTo(o2);
            }
        }
    });

    System.out.println(list);
}

From source file:MainClass.java

public static void main(String[] av) {

    String[] stringArray = new String[] { "c", "e", "a", "k" };

    ArrayList<String> list = new ArrayList<String>();
    for (String s : stringArray) {
        list.add(s);//from  w w  w .  j  a  va 2 s  . co  m
    }
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

    for (String s : list) {
        System.out.println(s);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // Lambda//from ww w  .j a  v a 2  s . co  m
    List<Person> persons = Arrays.asList(new Person("B", "V"), new Person("R", "K"));

    Collections.sort(persons, (Person a, Person b) -> {
        return b.firstName.compareTo(a.firstName);
    });
    Collections.sort(persons, (a, b) -> b.firstName.compareTo(a.firstName));
}

From source file:Main.java

public static void main(String args[]) {
    // create linked list object        
    LinkedList<Integer> list = new LinkedList<Integer>();

    // populate the list 
    list.add(-2);/* ww w .j a v a  2 s .c om*/
    list.add(2);
    list.add(-12);
    list.add(8);

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

    System.out.println("List sorted in natural order: ");
    System.out.println(list);

}

From source file:Main.java

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

    v.add("1");//www.ja  va2  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[]) {
    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);
}