scala map排序

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)

  }

你可能感兴趣的:(scala)