Alex Rivera | Logout

Does Scala have any collections that sort by value instead of key?

Asked 2011-08-06T22:12:03.530
14

For example the following code produces the order Stock1, Stock2, Stock3

var tm = TreeMap("Stock1" -> 4.2, "Stock3" -> 3.7, "Stock2" -> 5.9)

What I'm looking for is a collection or technique that can produce the following

Stock2, Stock1, Stock3 which represents the prices in descending order

Edit
Report

1 Answer

4

I don't think there is a built-in collection that keeps the map sorted by value so that you can read out the sorted values in linear time, but how to sort a scala.collection.Map[java.lang.String, Int] by its values? shows ways to get the entries out sorted by value.

answered 2011-08-06T22:16:52.830

Your Answer