3 Replies - 1460 Views - Last Post: 29 October 2011 - 07:16 PM Rate Topic: -----

#1 Java_student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Sorting a priority queue with multiple fields

Posted 29 October 2011 - 06:40 PM

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.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a priority queue with multiple fields

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,860
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#3 Java_student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-October 11

Re: Sorting a priority queue with multiple fields

Posted 29 October 2011 - 07:15 PM

Thank you very much for the help
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,860
  • Joined: 27-December 08

Re: Sorting a priority queue with multiple fields

Posted 29 October 2011 - 07:16 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1