你的意思是这样吗?
val v = Map("01" -> List(34,12,14,23), "11" -> List(22,11,34))
//v: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(34, 12, 14, 23), 11 -> List(22, 11, 34))
v map { case (k, v) => (k -> v.sorted) }
//res0: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(12, 14, 23, 34), 11 -> List(11, 22, 34))
编辑或:
v mapValues (_ sorted)
//res1: scala.collection.immutable.Map[String,List[Int]] = Map(01 -> List(12, 14, 23, 34), 11 -> List(11, 22, 34))
另一个编辑:
如果Ordering
您的元组中的所有类型都隐含在范围内,它应该以完全相同的方式工作:
val v = Map(
"01" -> List((34, "thirty-four"), (12, "twelve"), (14, "fourteen"), (23, "twenty-three")),
"11" -> List((22, "twenty-two"), (11, "eleven"), (34, "thirty-four")))
//v: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
// 01 -> List((34,thirty-four), (12,twelve), (14,fourteen), (23,twenty-three)),
// 11 -> List((22,twenty-two), (11,eleven), (34,thirty-four)))
v mapValues (_ sorted)
//res3: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
// 01 -> List((12,twelve), (14,fourteen), (23,twenty-three), (34,thirty-four)),
// 11 -> List((11,eleven), (22,twenty-two), (34,thirty-four)))
如果您只想按元组的一个成员排序(比如......在这种情况下是第二个):
v mapValues (_ sortBy (_ _2))
//res5: scala.collection.immutable.Map[String,List[(Int, String)]] =
//Map(
// 01 -> List((14,fourteen), (34,thirty-four), (12,twelve), (23,twenty-three)),
// 11 -> List((11,eleven), (34,thirty-four), (22,twenty-two)))