2014-01-16 29 views
0

我有一个数据库的查询作为一个ArrayCollection的对象返回。我想按字母顺序排序对象的一个​​属性。从db.queryResults()回来,对象属性的名称是DmvValue3。我如何对此进行排序。以下是我的代码和ArrayCollection中属性的屏幕截图。Flex移动动作脚本如何按字母顺序排序一个对象的一部分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

+0

我没有看到你打电话list.refresh()应用排序后。 –

+0

基本上我需要按字母顺序排序而不是第一个对象的每个对象的第三部分或属性。 – yams

+0

刷新与排序对象的某个属性无关。 – yams

回答

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 
    } 
+0

这不排序列表不幸的。 – yams

+0

您需要编辑您的问题并在数组集合的一个元素内发布一个项目的示例。您发布数据的屏幕截图不会告诉我数据对象是如何表示的。基本上我需要你向我展示数据结构。 –

+0

好吧,这是Arraycollection的第一个元素。 – yams