1、sorted
val a = List(1,4,2,3)
升序
val as = as.sorted
as:List[Int] = List(1,2,3,4)
逆序
val asr = as.sorted.reverse
asr:List[Int] = List(4,3,2,1)
2、sortBy
val b = List((1,-1),(2,-2))
升序
val bs = b.sortBy(_._2)
bs:List[(Int,Int)] = List((2,-2),(1,-1))
逆序
val bs = b.sortBy(_._2).reverse
bs:List[(Int,Int)] = List((1,-1),(2,-2))
3、sortWith,传入函数
val c = List("a","g","F")
升序
val cs = a.sortWith(_<_)
cs:List[String] = List("F","a","g")
逆序
val cs = a.sortWith(_<_).reverse
cs:List[String] = List("g","a","F")
传入函数
def comp(e1:String,e2:String):Boolean = {
e1.toLowerCase < e2.toLowerCase
}
val ds = c.sortWith(comp)
ds:List[String] = List("a","F","g")