Logo

Programming-Idioms

History of Idiom 113 > diff from v28 to v29

Edit summary for version 29 by berek:
New Rust implementation by user [berek]

Version 28

2019-10-08, 21:02:05

Version 29

2020-04-29, 09:36:39

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
Code
let mut items: Vec<_> = map.iter().collect();
items.sort_by_key(|item| item.1);
for (k, v) in items {
    println!("[{},{}]", k, v);
}
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2523003142f55a5403061cbbc7ad028c