标签:ring 集合排序 函数 排序 sorted val nbsp rev bsp
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")
标签:ring 集合排序 函数 排序 sorted val nbsp rev bsp
原文地址:https://www.cnblogs.com/xl717/p/11612327.html