
Copy link to clipboard
Copied
I'm trying to find a way to reverse the order that the for...in loop goes in, or reverse the index of variables on an object.
Example:
var o:Object = new Object();
o["a"] = {};
o["b"] = {};
for (var s:String in o)
{
trace(s);
}
this would output:
b
a
however, I need it to output:
a
b
The reason I need to do this is because I'm adding elements to the object as it loops, and I need it to loop through the elements in order rather than the elements that were just added.
In the actual script the for...in loop is nested in a while(true) infinite loop that breaks from within the for...in loop.
I've already tried using setChildIndex, but it doesn't work due to the children being arrays.
1 Correct answer
an object is an unordered collection of keys and values. it makes no sense to try and control the ordering of keys/values in an object.
you should use an array (or 2d array) if ordering is important.
Copy link to clipboard
Copied
an object is an unordered collection of keys and values. it makes no sense to try and control the ordering of keys/values in an object.
you should use an array (or 2d array) if ordering is important.

Copy link to clipboard
Copied
it's easy enough to work around it using this method:
var o:Object = new Object();o["a"] = {};o["b"] = {};
var a:Array = new Array();
for (var s:String in o){ a.push(s);}
for (var i:int = a.length-1; i >= 0; i--){ trace(a);}
...however, speed is a large factor in this algorithm; adding another "for" loop makes the CPU chug.... Objects are a collection of keys/values, however, flash still does order them by the order in which they were added. The problem is that the for...in loop lists them in reverse order. I'm open for ideas for speedy workarounds, unfortunately for the above, it takes over twice the time to execute.
Copy link to clipboard
Copied
again:
an object is an unordered collection of keys and values. it makes no sense to try and control the ordering of keys/values in an object.
you should use an array (or 2d array) if ordering is important.

Copy link to clipboard
Copied
I get what you're saying, an Array of Arrays. It's my fault for not specifying, but I need to be able to access an element by name rather than index. The names are rather specific and I have no way of knowing where an element will end up (how many elements will come before it or how many after) .
I have discovered a work around by going about the pathfinder problem differently that avoids this problem entirely, but if you think of a quick solution to this problem let me know, thanks!

