I am trying to order my Value Tuple on timestamp descending. My code is
import java.lang.{Double => JDouble}
def comparator(first: (Timestamp, JDouble), second: (Timestamp, JDouble)): Boolean = first._1.compareTo(second._1) < 1
val timeBoundContractRatesList: Map[String, List[(Timestamp, JDouble)]] = Map(
"ITABUS" -> List((Timestamp.valueOf("2021-08-30 23:59:59"), 0.8),
(Timestamp.valueOf("2021-09-30 23:59:59"), 0.9),
(Timestamp.valueOf("2021-07-30 23:59:59"), 0.7),
)
)
.map { case (key, valueTuple) => key -> valueTuple.sortWith(comparator) }.toMap
My expected output timeBoundContractRatesList should have values sorted in Descending order of timestamp,
Map(
"ITABUS" -> List((Timestamp.valueOf("2021-07-30 23:59:59"), 0.7),
(Timestamp.valueOf("2021-08-30 23:59:59"), 0.8),
(Timestamp.valueOf("2021-09-30 23:59:59"), 0.9),
)
)
But, I am not able to use the comparator function showing a datatype mismatch error. What is the efficient way to achieve this output?
CodePudding user response:
The doubles in your definitions (eg. in (Timestamp.valueOf("2021-09-30 23:59:59"), 0.9)
) are Scala doubles.
Either you remove the JDouble from the signature of the comparator or you should first convert the doubles to JDoubles