Flex Mobile actionscript如何按对象的一部分按字母顺序对arrayCollection进行排序

时间:2014-01-16 22:14:33

标签: actionscript-3 sorting flex flex-mobile arraycollection

我从db中获得了一个查询,该查询作为Objects的ArrayCollection返回。我想按字母顺序按对象的一个​​属性进行排序。从db.queryResults()返回,对象属性的名称是DmvValue3。我该如何排序呢。下面是我的代码和ArrayCollection中属性的截图。

        private function sortCollection(list:ArrayCollection):ArrayCollection
        {
            var sort:ISort = new Sort();
            var sortField:SortField = new SortField(null, true);
            sortField.setStyle("locale", "en-US");
            sort.fields = [sortField];
            list.sort = sort;
            list.refresh();
            return list;
        }

enter image description here

1 个答案:

答案 0 :(得分:1)

这是未经测试的代码,但它应该让您走上正确的道路。

private function sortCollection(list:ArrayCollection):ArrayCollection {
    var sort:ISort = new Sort(); 
    var sortField:SortField = new SortField(); 
    sortField.name = "DmvValue3"; 
    sortField.caseInsensitive = true; 
    sortField.numeric = true; 
    sortField.descending = true; 

    sort.fields = [sortField]; 

    list.sort = sort; 
    list.refresh(); 

    return list;
} 

[UPDATE]

    private function sortCollection(list:ArrayCollection):ArrayCollection {
        var sort:ISort = new Sort(); 
        var sortField:SortField = new SortField(); 
        //sortField.name = "DmvValue3"; 
        //sortField.caseInsensitive = true; 
        ////sortField.numeric = true; 
        //sortField.descending = true; 

        //sort.fields = [sortField]; 
        sort.compareFunction = myCompare;
        list.sort = sort; 
        list.refresh(); 

        return list;
    }
    public function myCompare(a:Object, b:Object, fields:Array = null):int {
        if(a["DmvValue3"] < b["DmvValue3"] )
            return -1; // -1, if a should appear before b in the sorted sequence
        if(a["DmvValue3"] == b["DmvValue3"] )
            return 0; // 0, if a equals b
        if(a["DmvValue3"] > b["DmvValue3"] )
            return 1; // 1, if a should appear after b in the sorted sequence
    }