1.自定义类实现Ordered
2. 实现compare 方法,compare方法里面实现自定义排序
3. 本例子中 先按照clickCount 排序, clickCount 相等 再按照orderCount 排序
4. 使用自定义排序 直接使用算子sortByKey,key是sortkey对象
//compare默认都是升序排列
case class **SortKey**(clickCount:Long,orderCount:Long,payCount:Long) extends Ordered[SortKey]{
override def compare(that: SortKey): Int = {
if(this.clickCount - that.clickCount!=0){
//clickCount不相等 按照clickCount 比较
return (this.clickCount - that.clickCount).toInt
}else if(this.orderCount -that.orderCount !=0){
//o按照clickCount 比较
return (this.orderCount - that.orderCount).toInt
}else{
//按照payCount 比较
return (this.payCount - that.payCount).toInt
}
}
}
val sortKey2FullCountRdd格式是: RDD[(**SortKey**, String)]
val top10Category: Array[(SortKey, String)] = sortKey2FullCountRdd.sortByKey(false).take(10)