Skip navigation
Currently Being Moderated

for in loop reverse order

Jul 18, 2012 7:51 PM

Tags: #for #as3 #in #loop #order

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.

 
Replies
  • kglad
    61,990 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2012 10:54 PM   in reply to Kyle517

    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.

     
    |
    Mark as:
  • kglad
    61,990 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 19, 2012 6:39 PM   in reply to Kyle517

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points