I have tuples of the form (Boolean, Int, String).
I want to define Ordering which sorts the tuples in the following order:
Boolean - revers
You could do something like this.
case class myTuple(t: (Boolean, Int, String)) extends Ordered[myTuple] {
def compare(that: myTuple):Int = {
val (x,y,z) =t
val (x1,y1,z1) = that.t
if (x.compare(x1) != 0) x.compare(x1)
else {
if (y.compare(y1) != 0) if (y.compare(y1) == 1) 0 else 1
else z.compareTo(z1)
}
}
}
val myList = Array((false, 8, "zz"), (false,3, "bb"), (true, 5, "cc"),(false, 3,"dd"))
implicit def tupleToBeordered(t: (Boolean, Int, String)) = new myTuple(t._1,t._2,t._3)
myList.sorted