scala map排序

来源:互联网 发布:js贪吃蛇 编辑:程序博客网 时间:2024/05/22 01:42
def main(args: Array[String]): Unit = {    val aMap = new scala.collection.mutable.HashMap[String, Double]    val a = Array("A", "B", "C", "D")    val b = Array(4, 5, 8, 9)    for(i <- a.indices){      aMap += (a(i) -> b(i))    }    // 从小到大(默认)    val mapSortSmall = aMap.toList.sortBy(_._2)    mapSortSmall.foreach(line => println(line._1 +"\t"+ line._2))    // 从大到小    val mapSortBig = aMap.toList.sortBy(-_._2)  }