I've defined a priority queue like so
import scala.collection.mutable.PriorityQueue
...
val queue = new PriorityQueue[(Int,Int)]()
I want to use this ordering:
If we are comparing two items A and B in the queue, A is bigger than B if the first element of its (Int,Int)
tuple is larger than B's. If they're the same, then A is bigger than B if the second element of its (Int,Int)
tuple is smaller than B's.
How do I define this kind of ordering?
If your elements are
Int
s, the easiest way to define such anOrdering
is by taking a negative of the elements which should be ordered in reverse.You can create the Ordering, using methods provided by the object
Ordering
, and pass it to thePriotityQueue
either explicitly:Or implicitly: