0

I've got a query from a db that is returned as a ArrayCollection of Objects. I want to alphabetically sort by one property of the object. Coming back from the db.queryResults() the name of the property of the object is DmvValue3. How to I sort this. Below is my code and a screenshot of the Property from the 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

ketan
  • 19,129
  • 42
  • 60
  • 98
yams
  • 942
  • 6
  • 27
  • 60

1 Answers1

1

This is untested code, but it should get you on the correct path.

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
    }
The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • This isn't sorting the list unfortunately. – yams Jan 17 '14 at 17:17
  • You need to edit your question and post an example of an item inside one element of the array collection. The screen cap that you posted of your data does not tell me how the data object is represented. Basically I need you to show me the data structure. – The_asMan Jan 17 '14 at 18:20
  • Ok there is the first element of the Arraycollection. – yams Jan 17 '14 at 18:48
  • Ok this object is a lot different then what you had me to believe earlier. I added an update to my answer you need to use the compare function. – The_asMan Jan 17 '14 at 19:13
  • Oh. Sorry about that. – yams Jan 17 '14 at 19:38
  • Ok I tried it and I got a ArgumentError: Error #1063: Argument count mismatch on com.amec.controls.Text::TextListView/myCompare(). Expected 2, got 3." – yams Jan 17 '14 at 19:47
  • Sorry can't test this code so i missed it change the function signature to this. function myCompare(a:Object, b:Object, fields:Array = null):int – The_asMan Jan 17 '14 at 20:38