使用来自2个不同字段的数值对arraycollection进行排序

时间:2023-01-07 07:44:18

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".

嗨伙计们,我需要对数组收集进行排序。问题是我的数组是由两个不同的数组组合而成,其中一个年龄字段被称为“AGE”,另一个年龄被称为“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?

现在我需要根据年龄对组合数组进行排序,但我得到两个具有不同名称的字段。我可以通过数组并将所有“MYAGE”更改为“AGE”,但我想知道是否有一种方法可以将数组排序为当前状态?

Thanks ahead

谢谢你

1 个解决方案

#1


1  

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

假设您有一个名为myCollection的ArrayCollection。我希望以下内容能解决您的要求:

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();

#1


1  

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

假设您有一个名为myCollection的ArrayCollection。我希望以下内容能解决您的要求:

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();