简体   繁体   中英

How can I sort values of type List[(Int, Int)] in Map?

I have this Map

Map(pop -> List((600,1), (2500,4)), classic -> List((500,0), (150,2), (800,3)))

And this is the result I want:

Map(pop -> List((2500,4),(600,1)), classic -> List((800,3),(500,0),(150,2)))

I want it to be sorted by the first index in List[(Int, Int)] , the value of map.
How can I achieve it?

You may use mapValues :

myMap.mapValues(_.sortBy(-_._1))

or:

myMap.mapValues(_.sorted.reverse)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM