UnestR
UnestR

Reputation: 11

How to sort a list of object type

Player class has been defined earlier. I want to sort a List of objects of type Player according to a particular attribute. However I am getting the the error : ')' expected for the following statement

Comparator <Player> compareBydefensiveDuel= (Player p1, Player p2)->(p1.getD().compareTo(p2.getD()));

I have also tried with the following statements:

Collections.sort(PlayerList,Comparator.comparing(Player ::getD());
PlayerList.sort(Comparator.comparingDouble(Player::getD()));

In all these cases it is asking for the ')' after Player. How to resolve this?

Upvotes: 0

Views: 48

Answers (1)

www.hybriscx.com
www.hybriscx.com

Reputation: 1129

With java 8 method reference, you must not use parentheses

Instead of Player::getD() Use Player::getD

Collections.sort(PlayerList,Comparator.comparing(Player::getD)); 

Upvotes: 1

Related Questions