Zulip Chat Archive
Stream: Is there code for X?
Topic: Sort a hashmap
Aaron Liu (May 06 2025 at 01:01):
I have a hashmap map : Std.HashMap ℕ α that has entries for all the keys up to a certain value, and I want to get an sorted : Array α out of this, such that sorted[n]? = map[n]? Is there an easy way to do this without converting the hashmap into a list, sorting it by the keys, and extracting the values?
Mario Carneiro (May 06 2025 at 01:05):
I would do it using Array.ofFn
Aaron Liu (May 06 2025 at 01:05):
I just figured out I can use docs#Array.toFn to do this, but I'm still looking for other solutions in case any are better.
Mario Carneiro (May 06 2025 at 01:07):
it doesn't seem like a very generalizable operation, so I don't see a big motivator to do better than ofFn (which I think is asymptotically optimal assuming you can work out the issues with proving lookups don't fail)
Aaron Liu (May 06 2025 at 01:07):
I think I'll just use map[n]!
Robin Arnez (May 06 2025 at 21:52):
if you had a tree map then this would just be map.valuesArray
Robin Arnez (May 06 2025 at 21:56):
although with a tree map suddenly lookups are O(log n) rather than the average O(1) for hash maps
Last updated: Dec 20 2025 at 21:32 UTC