Logo

Programming-Idioms

History of Idiom 113 > diff from v30 to v31

Edit summary for version 31 by programming-idioms.org:
[Rust] Variable names mymap, x

Version 30

2020-05-03, 19:53:03

Version 31

2020-05-03, 19:54:50

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.

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