Logo

Programming-Idioms

History of Idiom 113 > diff from v16 to v17

Edit summary for version 17 by :
New Prolog implementation by user [Boris]

Version 16

2016-02-16, 19:30:20

Version 17

2016-02-18, 16:58:03

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)
Imports
import Data.Map as Map
import Data.List (sortBy)
import Data.Ord (comparing)
Code
forM_ print $ sortBy (comparing snd) $ Map.toList mymap
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.

$ is apply operator and serves here only to save parentheses.
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.

$ is apply operator and serves here only to save parentheses.
Imports
import std.algorithm;
import std.array;
Imports
import std.algorithm;
import std.array;
Code
mymap.byKeyValue
     .array
     .sort!((a, b) => a.value < b.value)
     .each!(p => writeln(p.key, " ", p.value));
Code
mymap.byKeyValue
     .array
     .sort!((a, b) => a.value < b.value)
     .each!(p => writeln(p.key, " ", p.value));
Code
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";
}

Code
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";
}

Comments bubble
Create a custom, reusable sort routine and use it by name.
Assuming the keys are strings, we use <=> to compare the numeric values, and cmp to use the lexical order of the key as a tie breaker.
Comments bubble
Create a custom, reusable sort routine and use it by name.
Assuming the keys are strings, we use <=> to compare the numeric values, and cmp to use the lexical order of the key as a tie breaker.
Imports
import "fmt"
import "sort"
Imports
import "fmt"
import "sort"
Code
type entry struct {
	key   string
	value int
}

type entries []entry
func (list entries) Len() int { return len(list) }
func (list entries) Less(i, j int) bool { return list[i].value < list[j].value }
func (list entries) Swap(i, j int) { list[i], list[j] = list[j], list[i] }

entries := make(entries, 0, len(mymap))
for k, v := range mymap {
	entries = append(entries, entry{k, v})
}
sort.Sort(entries)

for _, e := range entries {
	fmt.Println("Key =", e.key, ", Value =", e.value)
}
Code
type entry struct {
	key   string
	value int
}

type entries []entry
func (list entries) Len() int { return len(list) }
func (list entries) Less(i, j int) bool { return list[i].value < list[j].value }
func (list entries) Swap(i, j int) { list[i], list[j] = list[j], list[i] }

entries := make(entries, 0, len(mymap))
for k, v := range mymap {
	entries = append(entries, entry{k, v})
}
sort.Sort(entries)

for _, e := range entries {
	fmt.Println("Key =", e.key, ", Value =", e.value)
}
Comments bubble
Define custom types entry and entries.
Then create a flat list of entries, and sort it.
Comments bubble
Define custom types entry and entries.
Then create a flat list of entries, and sort it.
Demo URL
http://play.golang.org/p/6o--5mK_xR
Demo URL
http://play.golang.org/p/6o--5mK_xR