0
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
}

'

3
  • 1
    for(Player j:playerQueue) does not guarantee order. Commented Jul 9, 2012 at 3:21
  • even taking a member variable as playerPrice is not helping me..I am still getting the order as random ?? Please help! Commented Jul 9, 2012 at 3:34
  • We've explained why it doesn't work. What more help do you need? Commented Jul 9, 2012 at 3:50

1 Answer 1

1

Note from the PriorityQueue API:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

So iterating through the Queue as you are doing will not be in "order".

The priority ordering becomes evident when you remove the head from the queue such as when you call poll() on the queue.

Sign up to request clarification or add additional context in comments.

2 Comments

At that point what might well be wanted is replacing the PriorityQueue with a TreeSet, which would yield the desired traversal order, and the players are presumably unique anyway so a set would be okay.
Thankyou will try the same and check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.