Sort on Multiple Fields in a Collection
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:s="library://ns.adobe.com/flex/spark">
<mx:ArrayCollection id="collection">
<mx:source>
<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" />
</mx:source>
</mx:ArrayCollection>
<mx:Script>
import mx.collections.Sort;
import mx.utils.ObjectUtil;
private function handleSort():void
{
var sort:Sort = new Sort();
sort.compareFunction = sortFunction;
collection.sort = sort;
collection.refresh();
}
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:Script>
<mx:DataGrid dataProvider="{collection}">
<mx:columns>
<mx:DataGridColumn dataField="name" />
<mx:DataGridColumn dataField="dob" />
</mx:columns>
</mx:DataGrid>
<mx:Button label="sort" click="handleSort();" />
</mx:Application>
Related examples in the same category