tuples - How to use priority queues in Scala? - Stack Overflow
There is actually pre-defined lexicographical order for tuples -- but you need to import it:
import scala.math.Ordering.Implicits._
Moreover, you can define your own ordering. Suppose I want to arrange tuples, based on the difference between first and second members of the tuple:
scala> import scala.collection.mutable.PriorityQueue // import scala.collection.mutable.PriorityQueue scala> def diff(t2: (Int,Int)) = math.abs(t2._1 - t2._2) // diff: (t2: (Int, Int))Int scala> val x = new PriorityQueue[(Int, Int)]()(Ordering.by(diff)) // x: scala.collection.mutable.PriorityQueue[(Int, Int)] = PriorityQueue()
Read full article from tuples - How to use priority queues in Scala? - Stack Overflow
No comments:
Post a Comment