【问题标题】:Sorting an arraycollection with a numeric value coming from 2 diffrent fields使用来自 2 个不同字段的数值对数组集合进行排序
【发布时间】:2011-08-10 11:24:05
【问题描述】:

大家好,我需要对数组集合进行排序。问题是我的数组是由两个不同的数组组合而成的,一个中的年龄字段称为“AGE”,另一个中的年龄字段称为“MYAGE”。

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

谢谢

【问题讨论】:

    标签: arrays apache-flex actionscript-3 sorting


    【解决方案1】:

    假设您有一个名为 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();
    

    【讨论】:

    • 如果 a 中的“AGE”不起作用(我已经看到文档说明它仅适用于动态对象属性),那么您也可以使用 .hasOwnProperty() 进行测试。示例:a.hasOwnProperty("AGE")。 adobe.ly/oVeR8m
    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多