需求:先按颜值降序排列,颜值相同,按照年龄升序排列
元数据
Array(("ningning", 90, 35), ("tingting", 90, 30), ("fangfang", 85, 19))
代码
package scalapackage.testsparkimport org.apache.spark.rdd.RDDimport org.apache.spark.{SparkConf, SparkContext}/** * Created by Germmy on 2018/5/23. */object MySort { implicit val girlOrdering = new Ordering[Girl] { override def compare(x: Girl, y: Girl): Int = { if (x.faceVal != y.faceVal) { x.faceVal - y.faceVal//按照颜值升序 } else { y.age - x.age } } }}object Test2CiPaixu { def main(args: Array[String]) { val sparkConf: SparkConf = new SparkConf().setAppName("Test2CiPaixu").setMaster("local[*]") val sc: SparkContext = new SparkContext(sparkConf) val girlInfo = sc.parallelize(Array(("ningning", 90, 35), ("tingting", 90, 30), ("fangfang", 85, 19))) // val by: RDD[(String, Int, Int)] = girlInfo.sortBy(_._3, false) //true升序,false降序 // println(by.collect().toBuffer) //方法一// import MySort.girlOrdering// val by: RDD[(String, Int, Int)] = girlInfo.sortBy(x => Girl(x._1, x._2, x._3), false) //期望结果:ArrayBuffer( (fangfang,85,19),(ningning,90,35), (tingting,90,30))// println(by.collect().toBuffer) //期望结果:ArrayBuffer((fangfang,85,19), (tingting,90,30)),(ningning,90,35) //方法二 val by: RDD[(String, Int, Int)] = girlInfo.sortBy(x => Girl(x._1, x._2, x._3), false) //期望结果:ArrayBuffer( (fangfang,85,19),(ningning,90,35), (tingting,90,30)) println(by.collect().toBuffer) //期望结果:ArrayBuffer((fangfang,85,19), (tingting,90,30)),(ningning,90,35) sc.stop() }}case class Girl(name: String, faceVal: Int, age: Int) extends Ordered[Girl]{ override def compare(that: Girl): Int = { if(this.faceVal!=that.faceVal){ this.faceVal-that.faceVal }else{ that.age-this.age } }}
注意:
1、无论是ordering还是ordered,都是x-y或者this-that,正数表示谁权重大,比如颜值,谁权重大表示谁就排在后面;但是年纪,如果用x-y>0,则表示年纪越大谁权重大,这与需求违背,所以应该反过来,用y-x,则为负数了,表示它权重小。
2、sortBy方法,默认为true,表示升序,即权重越大的排在后面。因此如果你想让颜值高的先显示,则用降序