如何对Scala中集合进行排序
2016-12-16
展开全部
如何对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
其实很简单代码如下:
// 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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询