Logo

Programming-Idioms

History of Idiom 113 > diff from v15 to v16

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

Version 15

2016-02-16, 19:23:03

Version 16

2016-02-16, 19:30:20

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.

Code
dict_pairs(D, _, Ps),
transpose_pairs(Ps, TPs),
keysort(TPs, Val_sorted),
forall(member(V-K, Val_sorted),
       format("~w:~w~n", [V, K]))
Comments bubble
Transpose the key-value pair list, sort by key (a stable sort), then print
Doc URL
swi-prolog.org
Demo URL
http://swish.swi-prolog.org/p/WKeHXAkd.swinb