Scala/Spark Ordering on multiple dimensions with reverse on specific one -
i order exemple 2d array or rdd follow :
val = array((1,1),(1,2),(1,3),(2,1),(2,2),(2,3)) to obtain ascending sort on d1 , descending 1 on d2 :
val b = array((1,3),(1,2),(1,1),(2,3),(2,2),(2,1)) unfortunately when apply reverse in ordering apply on 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 able sort on multiple dimension choising on 1 need reverse sorting.
this post contains answear scala idiom ordering 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 can help
Comments
Post a Comment