6 Replies Latest reply on May 27, 2009 12:58 PM by Flex harUI

    for...each...in loop.  Can I make assumptions about the order?

    Daniel Freeman Level 1

      I've always assumed that I can't make any assumptions about the order in which elements are indexed by a for...each...in loop.  Not even for a conventional indexed array.  (Emphasis!: I'm not talking about associative arrays here, I'm talking about normal indexed arrays.  Dense, no gaps.).


      for each (var element:ElementClass in myArray) {}


      May not traverse the array from first to last.  I've never assumed that it's guaranteed anyway.  Neither does the ECMA standard.  I assume the compiler/runtime is at liberty to index the array in any order, which may even be different each time. 


      Whereas... for (var i:int=0;i<myArray.length;i++) {......var element:ElementClass=myArray[i]....}


      ....Is guaranteed to traverse the array from first to last.  So I always use this looping statement if the ordering is important.


      MY QUESTION IS.... In ActionScript 3, Can I just assume that the for..each..in will always traverse a dense indexed array from first to last?


      Actually, my specific case at the moment is where another programmer is passing me an array,assembled within a for...each..in loop.   But I need to assume the order of that array.  If this is always going to work in ActionScript3, then no problem.  Although I'll continue to worry about the ECMA standard for my own code, and wont make assumptions about ordering - but I won't force my anal-retentive programming style on the other guy.