Scala Iterable[(String, Long)] to SortedMap[String,Long] or TreeMap[String, Long] sorted in decreasing order of value [closed]

You can’t do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can’t avoid converting to List:

 ListMap(i.toList.sortBy(-_._2):_*)

There isn’t really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.

Leave a Comment