gongo12
gongo12

Reputation: 19

Sorting an arraycollection with a numeric value coming from 2 diffrent fields

Hi guys I need to sort an arraycollection. the problem is that my array is combined from two diffrent arrays, in one the age field is called "AGE", and in the other the age is called "MYAGE".

Now I need to sort the combined array according to age but I got two fields with diffrent names. I can go through the array and change all "MYAGE" to "AGE" but i was wondering if theres maybe a way to sort the array in its current status?

Thanks ahead

Upvotes: 0

Views: 194

Answers (1)

Constantiner
Constantiner

Reputation: 14221

Say you have an ArrayCollection called myCollection. I hope the following will solve your request for that:

private function compareItems(a:Object, b:Object, fields:Array = null):int
{
    var firstValue:Number = "AGE" in a ? a["AGE"] : a["MYAGE"];
    var secondValue:Number = "AGE" in b ? b["AGE"] : b["MYAGE"];
    if (firstValue == secondValue)
        return 0;
    return firstValue > secondValue ? 1 : -1;
}

…

var sort:ISort = new Sort();
sort.compareFunction = compareItems;
myCollection.sort = sort;
myCollection.refresh();

Upvotes: 1

Related Questions