如何对Scala中集合进行排序
3 回答

翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
如何对Map中的Key或Value进行排序
其实很简单代码如下:
// sort by key can use sorted
m.toList.sorted foreach {
case (key,value) =>
println(key + " = " + value)
}
-2 = 5
-1 = -4
0 = -16
1 = 2
2 = 6
5 = 9
// sort by value
m.toList sortBy ( _._2 ) foreach {
case (key,value) =>
println(key + " = " + value)
}
0 = -16
-1 = -4
1 = 2
-2 = 5
2 = 6
5 = 9

智慧大石
TA贡献1946条经验 获得超3个赞
val list1 = List(1, 2, 3, 4, 5, 6)
list1.sorted
list1.sortWith(_ > _)
val sortedSet = scala.collection.immutable.SortedSet(2, 3, 4, 5, 1)
- 3 回答
- 0 关注
- 1296 浏览
添加回答
举报
0/150
提交
取消