Search code examples
apache-flexactionscript-3arraycollection

ArrayCollection reverses order of my objects


I'll show you the function first.

private var areaCollection:ArrayCollection;

private function generateAreaCollection():void
{
    areaCollection = new ArrayCollection();             
    areaCollection.addItem({Areal: "Totalareal:", Verdi: int(totalArea * 100) / 100 + " kvm"});
    areaCollection.addItem({Areal: "Hovedtakets areal:", Verdi: int(result.area* 100) / 100 + " kvm"});

    //Lots of other stuff omitted (just more addItems).
}

As you see, the order i put the items in the ArrayCollection is Areal, then Verdi (area, value)

When i loop through the collection later, the order changes. Now it is Verdi then Areal (value, area).

Does anyone have an idea of what might be the problem?

It really ruins things when I pass it over to a PHP-script for table-making in html later. (I have several different dynamic DataGrids that differs in size and "values", so I can't really point directly to f.ex "Areal" in my PHP-script)

And by the way, does anyone know how i can remove that pesky mx_internal_uid?


Solution

  • Raw objects in AS3 (Object class, or things defined by {} ), have no sorting. That is, the order is unpredictable. You're confusing two ideas here I think. The Areal and Verdi strings are keys within an object map. The array collection is a list composed of two such objects. So any sorting applied to the array collection will sort those objects, but not within the object.

    So you need to refer to areaCollection.source[0].Areal to get "Totalareal". And areaCollection.source[1].Verdi to get int(result.area* 100) / 100 + " kvm"

    If you do for(var s:String in areaCollection.source[0]) { } you will iterate twice, with the value of "s" being "Areal" and "Verdi" or vice-versa (e.g, order not guaranteed).

    Make sense? If you need order, you can make the object an array instead ["Totalareal", (int(totalArea * 100) / 100 + " kvm")], and then access "Verdi" using [1].

    P.s. not sure how to remove the mx_internal_id.