Sort int list in reversed order

Description

The following code shows how to sort int list in reversed order.

Example


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//from  w w  w .  j a v a2 s. c o  m

public class Main {
    public static void main(String[] args) {
      Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8 };
      List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

      Comparator<Integer> normal = Integer::compare;
      Comparator<Integer> reversed = normal.reversed(); 
      Collections.sort(listOfIntegers, reversed);
      
      listOfIntegers
          .stream()
          .forEach(e -> System.out.print(e + " "));
      System.out.println("");
      

    }
}

The code above generates the following result.





















Home »
  Java Streams »
    Examples »




Average
Filter
Group
IntStream
Map
Partition
Reduce
Sort
Sum