You want a max_by
comparison function which compares the first element to find which is greatest and the second to find which is smallest. Let's deal with the first criterion first: the first element should be largest.
a.0[0].cmp(&b.0[0])
a.0
and b.0
, as you've likely already surmised, select the key from the key-value pair provided. Then we use ordinary []
indexing to get the first element of the vector.
Now, I'm assuming for the purposes of the question that we want lexicographic ordering, also called dictionary order. That is, you've listed two criteria, and I'm assuming the first takes precedent, so that for instance [10, 2]
should be favorable to [9, 1]
, since the first element is bigger, despite the fact that the second is not smaller.
With that assumption in mind, we can expand our comparison. To reverse an ordering (i.e. to select a smallest rather than largest element) we can simply switch the order of arguments.
b.0[1].cmp(&a.0[1])
Then we only want to use this comparison if the first failed us, i.e. if the first was equal.
match a.0[0].cmp(&b.0[0]) {
Ordering::Equal => b.0[1].cmp(&a.0[1]),
x => x
}
If we put this in your max_by
function, we get [10, 1]
, as desired.
But we can actually do one better. See, lexicographic ordering is fairly common, so common that it's built-in to Rust. We can use the method Ordering::then
as follows.
a.0[0].cmp(&b.0[0]).then(b.0[1].cmp(&a.0[1]))
And this works identically to the previous example.
Complete example:
let mut queue : HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
queue.insert(vec![5 as u8, queue.keys().len() as u8], vec![0]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![1]);
queue.insert(vec![10 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![3 as u8, queue.keys().len() as u8], vec![2]);
queue.insert(vec![4 as u8, queue.keys().len() as u8], vec![3]);
queue.insert(vec![6 as u8, queue.keys().len() as u8], vec![4]);
let key = queue
.iter()
.max_by(|a, b| {
a.0[0].cmp(&b.0[0]).then(b.0[1].cmp(&a.0[1]))
})
.map(|(k, _v)| k);
println!("{:?}", key);