Flex ~ sort DataGrid date column

這問題是當要排序日期的時候發生用字串排序的問題

所以要用sortCompareFunction去把字串轉Date物件再來做比較~


MXML
<mx:DataGridColumn width="120" headerText="Submit Time" wordWrap="true" sortable="true" textAlign="left" dataField="SubmitTime" sortCompareFunction="sortDate">
</mx:DataGridColumn>


Action Script
(SubmitTime is one of dataProvider's field)

private function sortDate(obj1:Object, obj2:Object):int {
var dateA:Date = new Date(Date.parse(obj1.SubmitTime));
var dateB:Date = new Date(Date.parse(obj2.SubmitTime));
return ObjectUtil.dateCompare(dateA, dateB);
}



reference
http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/

沒有留言: