So I have something like:
class MyClass(val flag: Boolean, val value1: Double, val value2: Double)
And I would like to have a mutable PriorityQueue where objects of type MyClass are ordered according to a custom order, e.g.:
// I will only ever compare things with the same flag
def compare(this: MyClass, that: MyClass) = {
val temp = this.value1 compare that.value1
if(this.flag) temp = -temp // reversing the order of value1
if(temp != 0) temp else this.value2 compare that.value2
}
In words, each object has a flag and depending on that I'd like to order based on value1. If value1 is equal in both objects, then I only care about value2.
And then I'd like to have something like
val queue = new PriorityQueue[MyClass](...?
Thank you :)
Test case 1:
val queue = mutable.PriorityQueue[Order]()(Ordering.by{ord => (ord.price, ord.timestamp)})
val o1 = new Order(13, 3, idleStatus)
val o2 = new Order(11, 1, idleStatus)
val o3 = new Order(12, 2, idleStatus)
val o4 = new Order(15, 5, idleStatus)
val o5 = new Order(14, 4, idleStatus)
println(queue)
queue.enqueue(o1)
queue.enqueue(o2)
queue.enqueue(o3)
queue.enqueue(o4)
queue.enqueue(o5)
println(queue)
Prints the following:
PriorityQueue()
PriorityQueue((15, 5), (14, 4), (12, 2), (11, 1), (13, 3))
Which is wrong
It looks like maybe this is all you need.
val queue = mutable.PriorityQueue[MyClass]()(Ordering.by{mc =>
if (mc.flag) (-mc.value1,mc.value2) else (mc.value1,mc.value2)
})