# Priority Queue ordering issue

Page 1 of 1

## 3 Replies - 1227 Views - Last Post: 02 October 2011 - 09:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249584&amp;s=56541693ab097b3f9d8fe1e613b6f4a4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TroubledMonkey

Reputation: 1
• Posts: 2
• Joined: 02-October 11

# Priority Queue ordering issue

Posted 02 October 2011 - 09:05 PM

I'm using a Priority Queue for a project, but the results of "poll" are not what I'm expecting them to be. I have created a basic Coordinate class that stores and x and y value as well as f, g, and h values for implementing the A* algorithm. I wrote this as my compareTo method in my Coord class:
```public int compareTo(Coord c) {
return f - c.getF();
}

```

which should compare two coordinates by their f-values. However, in my A* algorithm, I have the lines:
```PriorityQueue<Coord> openSet = new PriorityQueue<Coord>();
.
.
.
while(!openSet.isEmpty()){
Coord x = openSet.poll();

```

but, for some reason, x is not getting set to the coordinate with the lowest f value. Sometimes it is, and sometimes it isn't. I've stepped through with the Eclipse debugger, and after a few iterations, there are elements in openSet with, for example, f = 20 and others with f = 23 and it will set x to one of the coordinate with f = 23. I'm really at a loss here. Is there some facet of Priority Queues in Java that I'm missing? Or could it be an issue somewhere else in my code?

Is This A Good Question/Topic? 0

## Replies To: Priority Queue ordering issue

### #2 TroubledMonkey

Reputation: 1
• Posts: 2
• Joined: 02-October 11

## Re: Priority Queue ordering issue

Posted 02 October 2011 - 09:19 PM

Shoot, sorry - I've actually found the problem and can't find a way to delete this post. (I really did spend a good hour in the debugger trying to fix this earlier today...glad it's finally fixed!)

If anyone's curious, my problem was that I was adding my new Coordinate y to the priority queue and then setting the f value once it was already in there, so it was getting sorted in the priority queue before it's f value had any actual meaning (defaulted to 0). Seems so obvious now... :-\

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,365
• Joined: 27-December 08

## Re: Priority Queue ordering issue

Posted 02 October 2011 - 09:20 PM

### #4 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: Priority Queue ordering issue

Posted 02 October 2011 - 09:22 PM

Can we see your code? because poll() returns head or null, I dont see if that is what you were expecting