Sort on Multiple Fields in a Collection : Sort « Data Model « Flex

Sort on Multiple Fields in a Collection

Sort on Multiple Fields in a Collection
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" 
    <mx:ArrayCollection id="collection">
                <mx:Object name="A" dob="08/17/2010" />
                <mx:Object name="B" dob="07/30/2011" />
                <mx:Object name="C" dob="07/30/2012" />
                <mx:Object name="D" dob="07/30/2013" />
            import mx.collections.Sort;
            import mx.utils.ObjectUtil;
            private function handleSort():void
                var sort:Sort = new Sort();
                sort.compareFunction = sortFunction;
                collection.sort = sort;
            private function sortFunction( a:Object, b:Object, fields:Array = null ):Boolean
                var dateA:Date = new Date( Date.parse( a.dob ) );
                var dateB:Date = new Date( Date.parse( b.dob ) );
                return ObjectUtil.dateCompare( dateA, dateB );
    <mx:DataGrid dataProvider="{collection}">
            <mx:DataGridColumn dataField="name" />
            <mx:DataGridColumn dataField="dob" />
    <mx:Button label="sort" click="handleSort();" />


Related examples in the same category

1.Use filter function and sortUse filter function and sort
2.Sort CollectionsSort Collections