How to get min by value only in Scala Map

Spread the love

Question Description

I have a map that has SomeClass -> Double I want to get the SomeClass associated with the smallest value. How do I go about doing this? Ties do not matter and can be broken arbitrarily if that is an issue.

Practice As Follows

Use minBy:

Map("a" -> 3.0, "b" -> 1.0, "c" -> 2.0).minBy(_._2)._1

This gives "b" as expected.

Tags

Not what you're looking for? Browse other questions tagged

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.