actionscript-3sortingapache-flexflex-mobilearraycollection

Flex Mobile actionscript how to alphabetically sort a arrayCollection by a portion of a object


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


Solution

  • 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
        }