0

I would like to order for exemple an 2d array or RDD like follow :

val a = Array((1,1),(1,2),(1,3),(2,1),(2,2),(2,3))

To obtain an ascending sort on d1 and a descending one on d2 :

val b = Array((1,3),(1,2),(1,1),(2,3),(2,2),(2,1))

Unfortunately when i apply reverse in the ordering it apply on all dimensions

a.sortBy( x=> (x._1,x._2) )(Ordering[(Int,Int)].reverse.on(x=> (x._1,x._2)))
Array((2,3), (2,2), (2,1), (1,3), (1,2), (1,1))

So i would like to be able to sort on multiple dimension choising on which one i need a reverse sorting.

KyBe
  • 842
  • 1
  • 14
  • 33

1 Answers1

0

This post contains the answear Scala idiom for ordering by multiple criteria

val ord1 = Ordering.by{ x:Int => x }
val ord2 = Ordering.by{ x:Int => x }.reverse
val multOrd = Ordering.by{ x:(Int,Int) => x }(Ordering.Tuple2(ord1,ord2))
a.sortBy( identity )(multOrd)
Array((1,3),(1,2),(1,1),(2,3),(2,2),(2,1))

Hope it can help

Community
  • 1
  • 1
KyBe
  • 842
  • 1
  • 14
  • 33