Run end-to-end properties of an object in flash memory

I have an object that receives properties added to this sequence.

Home School living status sound Memory 

When I go through an object, they do not exit in this sequence. How can I get them to go out in that order.

data is an object

 for (var i:String in data) { trace(i + ": " + data[i]); } 

Is there any way to sort it, maybe?

+4
source share
2 answers

The only way to sort the order in which you can access the properties is by hand. Here is the function I created for you to do just that:

 function getSortedPairs(object:Object):Array { var sorted:Array = []; for(var i:String in object) { sorted.push({ key: i, value: object[i] }); } sorted.sortOn("key"); return sorted; } 

Trial:

 var test:Object = { a: 1, b: 2, c: 3, d: 4, e: 5 }; var sorted:Array = getSortedPairs(test); for each(var i:Object in sorted) { trace(i.key, i.value); } 
+9
source

Link Marty shared what is going on pretty well, and his answer is also excellent.

Another solution that you can also consider when it comes to ordering is to use Vector .

 // Init the vector var hectorTheVector:Vector.<String> = new Vector.<String>(); // Can add items by index hectorTheVector[0] = "Home"; hectorTheVector[1] = "School"; hectorTheVector[2] = "living"; // Or add items by push hectorTheVector.push("status"); hectorTheVector.push("sound"); hectorTheVector.push("Memory"); //See all items in order for(var i:int = 0; i < hectorTheVector.length; i++){ trace(hectorTheVector[i]); } /* Traces out: Home School living status sound Memory */ 

An Array also keep order. here is a good topic when sorting arrays

+1
source

All Articles