Programming-Idioms

Implementation
Go

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Implementation edit is for fixing errors and enhancing with metadata.

Instead of changing the code of the snippet, consider creating another Go implementation.

Other implementations
sub by_val_then_key {
    return ($mymap{$a} <=> $mymap{$b})
        or ($a cmp $b)
}

for my $k (sort by_val_then_key keys %mymap) {
   print "$k: $mymap{$k}\n";
}

for my $k (sort {($mymap{$a}<=>$mymap{$b}) or ($a cmp $b)}
           keys %mymap) {
   print "$k: $mymap{$k}\n";
}
import std.algorithm;
import std.array;
mymap.byKeyValue
     .array
     .sort!((a, b) => a.value < b.value)
     .each!(p => writeln(p.key, " ", p.value));
import Data.Map as Map
import Data.List (sortBy)
import Data.Ord (comparing)
forM_ print $ sortBy (comparing snd) $ Map.toList mymap
dict_pairs(D, _, Ps),
transpose_pairs(Ps, TPs),
keysort(TPs, Val_sorted),
forall(member(V-K, Val_sorted),
       format("~w:~w~n", [V, K]))
h.sort_by{|k,x| x}.each{|k,x| puts "#{k}: #{x}"}
for x, k in sorted((x, k) for k,x in mymap.items()):
    print(k, x)
use itertools::Itertools;
for (k, x) in mymap.iter().sorted_by_key(|x| x.1) {
	println!("[{},{}]", k, x);
}
Object.entries(mymap)
  .sort((a, b) => a[1] - b[1])
  .forEach(([key, value]) => {
    console.log('key:', key, 'value:', value);
  });
map 
|> Map.to_list 
|> Enum.sort(fn ({_k1, val1}, {_k2, val2}) -> val1 <= val2 end)
|> Enum.each(fn ({k, v}) -> IO.puts("#{k}: #{v}") end)
 for_each(begin(m), end(m),
          [&s](const auto& kv) { s.insert(kv.second); });
using System.Collections.Generic;
using System.Linq;
Dictionary<String, String> mymap = new Dictionary<String, String>();
  
foreach(KeyValuePair<string, string> a in mymap.OrderBy(x => x.Value))
{
  Console.WriteLine("Key = {0}, Value = {1}", a.Key, a.Value);
}
let mut items: Vec<_> = mymap.iter().collect();
items.sort_by_key(|item| item.1);
for (k, x) in items {
    println!("[{},{}]", k, x);
}