Tamara Tamara - 4 months ago 9
Java Question

How to sort ArrayList<Long> in Java in decreasing order?

How to sort ArrayList< Long > in Java in decreasing order ?

Answer

Here's one way for your list:

Collections.sort(list);
Collections.reverse(list);

Or you could implement your own Comparator to sort on and eliminate the reverse step:

Collections.sort(list, new Comparator<Long>() {
    public int compare(Long o1, Long o2) {
        return o2.compareTo(o1);
    }
});

Or even more simply use Collections.reverseOrder() since you're only reversing:

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