一、文本格式(sort.txt)
1 5
2 4
3 6
1 3
2 1
二、编写类
package com.scala
/**
* scala处理二次排序的类
* 需要有参数
*/
class SecondSortByK(val first:Int,val second :Int) extends Ordered[SecondSortByK] with Serializable{
def compare(other:SecondSortByK):Int={
//this关键字可加,也可不加,如果遇到多个变量时,必须添加
if(this.first-other.first!=0)
this.first-other.first
else
this.second-other.second
}
//重写toString方法
override def toString():String={
"first:"+first+" second:"+second