If I have:
my_hash_map: HashMap<usize, MyStruct>,
and I do:
my_hash_map.iter()
I get a HashMapIter<'_, usize, MyStruct>
that iterates in arbitrary order.
Is there a way to iterate in the order of the keys? They are ordered because the key is a usize
tczajka
2
Change your type to BTreeMap<usize, MyStruct>
which stores the keys in order. HashMap doesn't store keys in order.
4 Likes
alice
3
Another option is to collect the keys into a vector and sort it.
1 Like
system
Closed
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.