Is there a way to sort a dictionary by key, so that if I pull out an array from it via keys() or values() (or just iterate over it with a for-loop) I get things in a specific order? Like, say I wanted to use a dictionary to store my inventory, like this:
var inventory = {"Bomb": 2, "Ether": 1, "Potion": 5}
So far, it's alphabetical, so if I use a for-loop to display that inventory to the player, it'll be in the correct order. But if I pick up 20 arrows, just going:
inventory["Arrow"] = 20
Will, if I'm understanding correctly, put it at the end of the list because that's when it was added. Is there an easy way to tell it it should put it at the appropriate spot in alphabetical order (or a custom order) so that when I try to use it, I get the same list regardless of which things were added when? I checked the documentation for dictionaries and it doesn't mention any kind of sort function. So far the best way I've come up with to do that is to have a separate, pre-sorted array with the correct order, then just iterate over that instead and use an if to check if each item in the array has a value in the dictionary, but that seems really inefficient and I assume I'm missing something much more straightforward.