PriorityQueue<Player> playerQueue = new PriorityQueue<Player>(30,
new Comparator<Player>( ) {
// overriding the compare method
public int compare(Player i, Player j) {
return (int)i.playerPrice() < (int)j.playerPrice() ? -1 : ((int)i.playerPrice() == (int)j.playerPrice()) ? 0 : 1;
}
}
);
for(Player a:XMLParserViaSax.getListOfPlayers()){
System.out.println(a.getName()+ "-->"+a.playerPrice());
}
for(Player player:XMLParserViaSax.getListOfPlayers()){
playerQueue.add(player);
}
//playerQueue.addAll(XMLParserViaSax.getListOfPlayers());
System.out.println("**************************");
for(Player j:playerQueue){
System.out.println(j.getName()+ "-->"+j.playerPrice());
}
I am trying to sort the Player by playerprice,I don't have any variable as playerprice in Player class.playerPrice() is a method which will calculate price of a player.so now when I am trying to sort these Player Objects it is giving me a random order.
Do I need to have one playerprice private member inside my player class ? ' Player class is like this :
public class Player {
private String name;
private int battingStrength;
private int bowlingStrength;
private int fieldingStrength;
private int keepingStrength;
private int jerseyNumber;
public int playerStrength() {
return AuctionPlayerHelper.calculateStrength(this);
}
public boolean canKeepWickets() {
return (keepingStrength == 10);
}
public boolean isForeignPlayer(){
return AuctionPlayerHelper.isForeignPlayer(this.jerseyNumber);
}
public double playerPrice() {
return AuctionPlayerHelper.calculatePrice(this);
}
//getters,setters
}
'
for(Player j:playerQueue)does not guarantee order.