The ActionScript dictionary class is not really an ordered collection. In other words, it really does not have a โfirst elementโ. Of course, if you iterate over elements using a for or for each loop, you first need to access one of the elements so that you can do something like this:
var firstKey:*; var firstValue:*; for (firstKey in myDictionary) { firstValue = myDictionary[firstKey]; break; } // variables now hold the "first" key and value, or undefined if the dictionary is empty.
Nevertheless, the Dictionary class does not have a contract on the order of elements, and even if the order was predictable (in alphabetical order, first == first return, etc.), I would consider its implementation detail, which can be changed between player versions . In addition, the class does not provide any internal map that you can access.
If you really care about controlling the iteration order (or just want to understand how it is handled in ActionScript), I would suggest checking out the Proxy Class . By expanding it and redefining several methods, you can control the order in which elements are repeated, as well as some other interesting things. (The methods associated with iteration, nextName () , nextNameIndex (), and nextValue () , are examples in the official documentation .) This way you can create your own dictionary-like collection class that also indexes elements.
Alternatively, you can look at third-party Collection / Iterator packages, which may already have something like what you need.
source share