Logo

Programming-Idioms

History of Idiom 113 > diff from v13 to v14

Edit summary for version 14 by :
New Haskell implementation by user [JH]

Version 13

2016-01-09, 18:04:14

Version 14

2016-02-16, 19:21:30

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Imports
import Data.Map as Map
import Data.List (sortBy)
import Data.Ord (comparing)
Code
forM_ print $ sortBy (comparing snd) $ Map.toList mymap
Comments bubble
Map.toList gives a list of all (key,value) pairs. We sort that list on the values (second item of the pairs) and prints that.