Logo

Programming-Idioms

History of Idiom 113 > diff from v20 to v21

Edit summary for version 21 by GobbleCock:
New Rust implementation by user [GobbleCock]

Version 20

2017-10-28, 12:30:49

Version 21

2018-12-27, 21:41:16

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.

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Imports
use itertools::Itertools;
Code
for (k, v) in map.iter().sorted_by_key(|x| x.1) {
	println!("[{},{}]", k, v);
}
Comments bubble
Requires the itertools crate
Doc URL
https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.sorted_by_key
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=076ae1542298014fb93a0259362c8652