Hey hows it going?
I am having problems with a certain aspect of the priority queues. I built an Ordered Array priority queue which successfully returns the object with the highest priority key. However I have to sort the elements not just by the priority key but also by the category. So for all of the elements with priority 1, I need them to be removed in order of category as well. For example If I had three elements with priority 1 but one was a cat, one was a dog, and one was a turtle. Turtles have to be removed first.
I am stumped on how to approach this. I have read more about comparators but it appears that i would have to adjust the Ordered Array Priority Queue data structure which i am not allowed to do. So I need to figure out some way to do it without using that approach. My other idea was to some how incorporate this extra sort into the compareTo method. But I dont know how to do that without ruining the sort that it is doing successfully.
My latest idea was to assign an integer to every category and then compare them to each other but I dont know how I could do that while still incorporating the priorities.
Any help on this would be greatly appreciated, all of my attempts so far have failed so any push in the right direction would be great.
Page 1 of 1
3 Replies - 1518 Views - Last Post: 29 October 2011 - 07:16 PM
Replies To: Sorting a priority queue with multiple fields
Re: Sorting a priority queue with multiple fields
Posted 29 October 2011 - 07:09 PM
You will want to modify the compareTo() method. If the categories aren't the same, then compare them on their categories. Otherwise, compare them on the priority field.
Page 1 of 1