Lucas Marinzeck
Lucas Marinzeck

Reputation: 343

Why cant i sort in a reverse order in this code?

I was trying to sort this ArrayList<Person> in a reverse order but this does not compile

    List<Person> newList = arrayList.stream()
                .sorted(Comparator.reverseOrder(Person::getAge)) //Error
                .limit(3)
                .collect(Collectors.toList());

    newList.forEach(System.out::println);

Is there any other way to sort streams in a reverse order?

Upvotes: 0

Views: 106

Answers (1)

Adrian
Adrian

Reputation: 3134

Here is the correct way to use it:

.sorted(Comparator.comparing(Person::getAge).reversed())

Upvotes: 5

Related Questions