I would like to sort an array of tuples by 3rd and first element so I used the following code:
import scala.util.Sorting
val pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3))
// sort by the 3rd element, then 1st
Sorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1)))
My question is that, in the previous example I can sort by both the third and the first elements ascending or both of them descending (use reverse). but how to sort by the third element ascending and the 1st element descending.
Please, in your answer consider the following case:
Array[Array[People]]
where in this case I do not know the exact size of the inner array (depends on the file schema that I read into this array) and I want to sort by all the items in side (some ascending and some descending).
Edit: It looks like, i got miss understood.
Here is my full case: I have the following classes:
sealed trait GValue extends Serializable with Ordered[GValue]{
def compare(o: GValue): Int = {
o match {
case GDouble(v) => this.asInstanceOf[GDouble].v compare v
case GString(v) => this.asInstanceOf[GString].v compare v
}
}
case class GDouble(v: Double) extends GValue
case class GString(v: String) extends GValue
and i want to do a code like this.
// (startInterval, StopInterval, ArrayOfColumns)
val intervals: Array[(Long,Long,Array[GValue])] =
Array((10,20,Array(GDouble(10.2), GString("alarm"), GString("error"),GDouble("100.234"))),
(30,2000,Array(GDouble(-10.2), GString("alarm"), GString("warn"),GDouble("0.234"))))
The schema or the inner array would change based on the input file (in the example it is Double,String, String, Double but it might be Double, Double or something else). I would like to find a way to sort with out covering all the cases of the inner array (in regards to type and length), ascending and descending.
what i do currently is to change the inner array to Iterable and then use Ordering[Iterable[GValue]] for sorting or Ordering[Iterable[GValue]].reverse. But i want to sort in separated directions (ascending for the first column and then descending for the second then ascending to the third and so on)