Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

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.
Multiple entries may exist for the same value x.

Object.entries(mymap)
  .sort((a, b) => a[1] - b[1])
  .forEach(([key, value]) => {
    console.log('key:', key, 'value:', value);
  });
for_each(begin(mymap), end(mymap),
    [&s](const auto& kv) { s.insert(kv.second); });

Using a std::multiset<>

New implementation...
< >
programming-idioms.org