按照key和value升序、降序、自定义排序示例
use std::collections::HashMap;
use std::cmp::Ordering;
fn main() {
let mut df = HashMap::new();
for x in 5..=12 {
let k = format!("key_{}",x);
let v = format!("value_{}",x);
df.insert(k,v);
}
println!("{:#?}",df);
let mut tmp : Vec<(&String, &String)>= df.iter().collect();
// 按照key排序
tmp.sort_by_key(|x| x.0); // 升序 eq => tmp.sort_by(|x,y| x.0.cmp(y.0));
println!("key up:\t{:?}",tmp);
tmp.sort_by(|x,y| y.0.cmp(x.0)); // 降序
println!("key down:\t{:?}",tmp);
// 按照value排序
tmp.sort_by(|x,y|x.1.cmp(y.1)); // 升序
println!("value up:\t{:?}",tmp);
tmp.sort_by(|x,y|y.1.cmp(x.1)); // 降序
println!("value down:\t{:?}",tmp);
// 人类习惯的方式排序
tmp.sort_by(|x,y| myorder(x.0,y.0)); // key 升序
println!("key up:\t{:?}",tmp);
tmp.sort_by(|x,y| myorder(y.0,x.0)); // key 降序
println!("key down:\t{:?}",tmp);
tmp.sort_by(|x,y| myorder(x.1,y.1)); // value升序
println!("value up:\t{:?}",tmp);
tmp.sort_by(|x,y| myorder(y.1,x.1)); // value 降序
println!("value down:\t{:?}",tmp);
}
fn myorder(a: &String, b: &String) -> Ordering {
// split_mix: 返回混合字符串的字符和数字部分
let (p1,n1) = split_mix(a);
let (p2,n2) = split_mix(b);
match p1.cmp(&p2) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => n1.cmp(&n2) //字符串相同时继续比较数字部分
}
}
fn split_mix(k: &String) -> (String, u32){
let num: String = k.chars().filter(|x| x.is_digit(10)).collect();
let strs: String = k.chars().filter(|x| !x.is_digit(10)).collect();
(strs ,num.parse().unwrap())
}
输出:
{
"key_12": "value_12",
"key_5": "value_5",
"key_11": "value_11",
"key_7": "value_7",
"key_9": "value_9",
"key_8": "value_8",
"key_10": "value_10",
"key_6": "value_6",
}
key up: [("key_10", "value_10"), ("key_11", "value_11"), ("key_12", "value_12"), ("key_5", "value_5"), ("key_6", "value_6"), ("key_7", "value_7"), ("key_8", "value_8"), ("key_9", "value_9")]
key down: [("key_9", "value_9"), ("key_8", "value_8"), ("key_7", "value_7"), ("key_6", "value_6"), ("key_5", "value_5"), ("key_12", "value_12"), ("key_11", "value_11"), ("key_10", "value_10")]
value up: [("key_10", "value_10"), ("key_11", "value_11"), ("key_12", "value_12"), ("key_5", "value_5"), ("key_6", "value_6"), ("key_7", "value_7"), ("key_8", "value_8"), ("key_9", "value_9")]
value down: [("key_9", "value_9"), ("key_8", "value_8"), ("key_7", "value_7"), ("key_6", "value_6"), ("key_5", "value_5"), ("key_12", "value_12"), ("key_11", "value_11"), ("key_10", "value_10")]
key up: [("key_5", "value_5"), ("key_6", "value_6"), ("key_7", "value_7"), ("key_8", "value_8"), ("key_9", "value_9"), ("key_10", "value_10"), ("key_11", "value_11"), ("key_12", "value_12")]
key down: [("key_12", "value_12"), ("key_11", "value_11"), ("key_10", "value_10"), ("key_9", "value_9"), ("key_8", "value_8"), ("key_7", "value_7"), ("key_6", "value_6"), ("key_5", "value_5")]
value up: [("key_5", "value_5"), ("key_6", "value_6"), ("key_7", "value_7"), ("key_8", "value_8"), ("key_9", "value_9"), ("key_10", "value_10"), ("key_11", "value_11"), ("key_12", "value_12")]
value down: [("key_12", "value_12"), ("key_11", "value_11"), ("key_10", "value_10"), ("key_9", "value_9"), ("key_8", "value_8"), ("key_7", "value_7"), ("key_6", "value_6"), ("key_5", "value_5")]
标签:tmp,10,12,HashMap,value,key,11,排序,rust
From: https://www.cnblogs.com/mmtinfo/p/17757387.html