方式1 :隐式值 (remark:隐式 调用 顺序 隐式Object > 隐式值> 隐式函数> 隐式方法)
def main(args: Array[String]): Unit = { val sparkConf = new SparkConf().setMaster("local[2]").setAppName("customersort1") val sc = new SparkContext(sparkConf) val girlInfo = sc.parallelize(Array("xiaoming 90 31","xiong 90 32","dabai 80 34","polu 98 35")) val pesion = girlInfo.map(x=> { val arr = x.split(" ") val name = arr(0) val age = arr(2).toInt val fv = arr(1).toInt (name,fv,age) }) //隐式值 //[(Int,Int) 要比较的参数
//按照fv反排序,如果fv 相同 按照 age 正排序 (-x._2,x._3)
implicit val oder= Ordering[(Int,Int)].on[(String,Int,Int)](x=>(-x._2,x._3)) val buffer = pesion.sortBy(x=>x).collect.toBuffer println(buffer) sc.stop() } }
方式2: 元组的封装
//按照fv反排序,如果fv 相同 按照 age 正排序 (-x._2,x._3)
val buffer = pesion.sortBy(x =>(-x._2,x._3)).collect.toBuffer