• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a Priority Queue  RSS feed

 
Connor Cartwright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm at a loss on how to do this and keep getting confused looking at examples.

I have a PriorityQueue called incoming, containing objects of type Vehicle.

All vehicles have a fuel level, that can be accessed by the getter method getFuelLevel().

I want to be able to choose to sort the queue so that the Vehicles with the lowest fuel levels are at the front of the queue.

Any help is much appreciated.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you click on that auto-generated link in your post it will take you to the Javadoc for the PriorityQueue class. There it explains how to get the elements of the Queue sorted
The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time

To give your Vehicle class a natural ordering you have to change it so that it implements the Comparable interface. If that's not possible you will need to supply an instance of a Comparator class when you create the PriorityQueue.
See this tutorial for more information.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
Connor Cartwright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment I have:

public class FuelPriority implements Comparator<Object> {

public int compare(Object o1, Object o2) {

Aircraft a1 = (Aircraft) o1;
Aircraft a2 = (Aircraft) o2;

return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel());
}
}

And then when I initialize the PriorityQueue I do:
private FuelPriority strategy;

incoming = new PriorityQueue<IAircraft>(500, strategy);

My problem is that it doesn't seem to be sorting the queue right; below is the output for 3 separate seeds of the simulation:

13, 16, 27, 21, 19, 39, 37, 50, 30, 34, 64, 46, 49, 72, 59, 58, 55, 61, 47, 73

8, 13, 11, 19, 26, 29, 39, 22, 47, 53, 42, 31, 38, 41, 56, 64, 50, 51, 65

6, 13, 14, 16, 25, 26, 17, 22, 39, 28, 31, 40

It seems to sort the queue haphazardly.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connor Cartwright wrote:
And then when I initialize the PriorityQueue I do:


Are you initialising your strategy variable ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connor Cartwright wrote:It seems to sort the queue haphazardly.

TBH, I'm surprised it's sorting it at all, because I don't see any place that you've initialised 'strategy'.
Try:
private FuelPriority strategy = new FuelPriority();
or something like it.

Winston
 
Connor Cartwright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Are you initialising your strategy variable ?


Winston Gutkowski wrote:
TBH, I'm surprised it's sorting it at all, because I don't see any place that you've initialised 'strategy'.
Try:
private FuelPriority strategy = new FuelPriority();
or something like it.

Winston


I hadn't been initialising strategy, but after initialising it I still get similar output:

6, 13, 17, 14, 16, 26, 28, 22, 39, 31, 25, 40

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connor Cartwright wrote:I hadn't been initialising strategy, but after initialising it I still get similar output:
6, 13, 17, 14, 16, 26, 28, 22, 39, 31, 25, 40

And this means what? Looks moderately "ascending" to me (give or take), but we have no idea what those numbers represent, and we still have no idea what you're supplying to that queue or how you're doing it.

TellTheDetails (←click).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!