注意:
Map 集合没有可以直接进行排序的方法,必须先转换为List集合,然后键值对以元组的形式存放在List集合中
案例演示
def main(args: Array[String]): Unit = {
val map = Map("a" -> 2, "b" -> 1, "c" -> 3)
val ints: List[(String, Int)] = map.toList.sortWith {
case (t1, t2) => {
t1._2 > t2._2
}
}
println(ints)
}
输出:
List((c,3), (a,2), (b,1))