Example usage for java.util List sort

List of usage examples for java.util List sort

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
default void sort(Comparator<? super E> c) 

Source Link

Document

Sorts this list according to the order induced by the specified Comparator .

Usage

From source file:Main.java

public static void main(String... args) {

    List<Integer> numbers = Arrays.asList(3, 5, 1, 2, 6);
    numbers.sort(Comparator.naturalOrder());
    System.out.println(numbers);/*w  ww .  j  a va2s  .c  o m*/

}

From source file:Main.java

public static void main(String... args) {
    List<String> names = Arrays.asList("XML", "Java", "HTML", "CSS");
    names.sort(Collections.reverseOrder());
    System.out.println(names);//w ww . j a va  2s  .com
}

From source file:Main.java

public static void main(String... args) {
    List<String> names2 = Arrays.asList("XML", null, "Java", "HTML", "CSS");
    names2.sort(Comparator.nullsLast(String::compareTo));
    System.out.println(names2);//from   ww w  .  j a  v  a2s .com

}

From source file:Main.java

public static void main(String... args) {
    List<String> names2 = Arrays.asList("XML", null, "Java", "HTML", "CSS");
    names2.sort(Comparator.nullsFirst(String::compareTo));
    System.out.println(names2);/*from   ww w . j a va  2  s .c  o  m*/

}

From source file:Main.java

public static void main(final String[] args) {
    List<String> names2 = Arrays.asList("peter", null, "anna", "mike", "xenia");
    names2.sort(Comparator.nullsLast(String::compareTo));
    System.out.println(names2);/*from  w  w  w  . jav a 2 s  .  c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    // setup//w ww. j  a va  2  s.co m
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way, the shortest way
    list.sort((a, b) -> a - b);
    System.out.println(list);

}

From source file:Main.java

public static void main(String[] args) {
    // setup/*from   ww  w  .  ja v a 2s  .com*/
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way
    Comparator<Integer> lambdaComparator = (Integer a, Integer b) -> b - a;
    list.sort(lambdaComparator);
    System.out.println(list);

}

From source file:Main.java

public static void main(String[] argv) {
    List<Person> persons = Arrays.asList(new Person("Joe", 12), new Person("Jim", 34), new Person("John", 23));
    persons.sort((p1, p2) -> p1.getFirstName().compareTo(p2.getFirstName()));
    persons.forEach(p -> System.out.println(p.getFirstName()));
}

From source file:Main.java

public static void main(String... args) {
    List<Apple> inventory = new ArrayList<>();
    inventory.addAll(Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")));

    inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));
    System.out.println(inventory);
}

From source file:Main.java

public static void main(String... args) {
    List<Apple> inventory = new ArrayList<>();
    inventory.addAll(Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")));

    inventory.sort(comparing(Apple::getWeight));
    System.out.println(inventory);
}