How would you sort a scala.collection.Map[java.lang.String, Int] by its values (so on the Int)? What is a short and elegant way to do that?
How would you sort a scala.collection.Map[java.lang.String, Int] by its values (so on the Int)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depending on what the expected output collection type is (
SortedMaps are sorted on the keys), you could use something like this:Result would be the list of key/value pairs sorted by the value:
There is a Map type that retains the original order,
ListMap, if you apply this, you have a map again:Then you have:
(Scala 2.8)