【问题标题】:Flex Mobile actionscript how to alphabetically sort a arrayCollection by a portion of a objectFlex Mobile actionscript如何按对象的一部分按字母顺序对arrayCollection进行排序
【发布时间】:2014-01-16 22:14:32
【问题描述】:

我有一个来自 db 的查询,它作为对象的 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;
        }

【问题讨论】:

  • 我没有看到你在应用排序后调用 list.refresh()。
  • 基本上每个对象的第三部分或属性我需要按字母顺序排序,而不是第一个。
  • 刷新与对象的某一属性排序无关。

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


【解决方案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;
} 

[更新]

    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
    }

【讨论】:

  • 不幸的是,这没有对列表进行排序。
  • 您需要编辑您的问题并在数组集合的一个元素中发布一个项目示例。您发布的数据的屏幕截图并没有告诉我数据对象是如何表示的。基本上我需要你给我看数据结构。
  • 好的,Arraycollection 的第一个元素就出来了。
  • 好吧,这个对象和你之前让我相信的有很大不同。我为我的答案添加了更新,您需要使用比较功能。
  • 哦。对此感到抱歉。
猜你喜欢
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 2013-10-28
  • 2019-06-15
  • 2011-08-29
  • 2023-01-30
  • 1970-01-01
  • 2010-11-23
相关资源
最近更新 更多