Archive for May, 2013

Sort Arraycollection based on date field in Flex

If you have ArrayCollection as Dataprovider to Datagrid and you want to sort dataprovider based on single date field for ex: created_Date or modified_Date, then below is code snippet:

public function sortOnFilterModifiedDate():void
{
var dataSortField:SortField = new SortField();
dataSortField.name = “modified_Date”;//date field
dataSortField.numeric = false;
dataSortField.descending = true;//true or false do not affect sorting logic
dataSortField.caseInsensitive = true;

var filterModifiedDateSort:Sort = new Sort();
filterModifiedDateSort.fields = [dataSortField];
filterModifiedDateSort.compareFunction = filterModifiedDateSortCompareFunc;

pageFilters.sort = filterModifiedDateSort;
pageFilters.refresh();

}

private function filterModifiedDateSortCompareFunc (itemA:Object,itemB:Object,fields:Array):int
{
return ObjectUtil.dateCompare(DateFormatter.parseDateString(itemA.modified_Date),

DateFormatter.parseDateString(itemB.modified_Date));
}

Use below code to sort in Descending order: -1*ObjectUtil.dateCompare(DateFormatter.parseDateString(itemA.modified_Date),

DateFormatter.parseDateString(itemB.modified_Date));

,

Leave a comment