I think we would be able to help you better if you told us exactly what you are questioning about this code. Otherwise, we might spend a lot of time explaining details you already know, and maybe even missing the details you need help with.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Feb 17, 2008
I am not able to follow how the compare method works. And how the values are compared and loaded in the queue. K&B says we follow opposite of the natural ordering. But I don see the output that way.
The compare(o1, o2) method returns an int indicating how o1 and o2 compare to each other. If o1 should come before o2, then a negative int is returned. If o1 should come after o2, then a positive int is returned. If o1 and o2 are "equal," then zero is returned.
In this example, the compare method returns the difference of (o2 - o1).
So if o1 is greater than o2, then a negative int is returned, indicating that o1 should come before o2. On the other hand, if o1 is less than o2, then a positive int is returned, indicating that o1 should come after o2.
Because the method subtracts o1 from o2 (instead of the other way around), it gives the opposite of natural ordering, because it puts larger values before smaller values.