运维开发网

scala集合排序

运维开发网 https://www.qedev.com 2020-07-31 14:50 出处:网络 作者:运维开发网整理
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

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(_<_).reversecs: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")

0

精彩评论

暂无评论...
验证码 换一张
取 消