I have the following class :
final case class PowerDetails (
[...],
label: String,
title: String,
category: String,
idCategory: Option[String],
numCategory: Option[Int]
)
I need to sort a list of PowerDetails like this :
Elements having a value for
idCategoryandnumCategoryshould be before theNone, and ordered byidCategoryand thennumCategory. As for theNonevalues, they need to be ordered by the fieldscategory,title,label.
If possible, I would like to avoid extending Ordered
final case class PowerDetails (
...
) extends Ordered[PowerDetails] {
override def compare(that: PowerDetails): Int = {
// Define here the comparison
}
}
I tried creating an Ordering as below and then use the sorted function, but I'm getting a NullPointerException
implicit val orderingPowers = optionOrdering.thenComparing(ordering)
val ordering: Ordering[PowerDetails] = Ordering[(String, String, String)]
.on[PowerDetails](powerDetail =>
( powerDetail.category, powerDetail.title, powerDetail.label))
val optionOrdering : Ordering[PowerDetails] = Ordering[(Option[String], Option[Int])]
.on[PowerDetails](powerDetail =>
(powerDetail.idCategory, powerDetail.numCategory))
Could you please help me find out how to do it ? Thanks.