Common Date Field Sorting for multiple columns in DataGrid, Flex

Hi All,

It is very easy to attach a common sorting function in data grid on the multiple String date field columns or any other type columns(multiple),

Below I am giving en example on attaching common sorting function on date field.

My example contains a data grid which is having two columns with String date. And for that I have written  a generic function which read the header text of the columns and attach sorting function if the header text contains “date” text.

public static function commonSortFunction(myGridObj:DataGrid):void{
if(myGridObj!=null && myGridObj.dataProvider.length > 0){
for(var j:int = 0; j< myGridObj.columnCount; j++){
if((myGridObj.columns[j] as DataGridColumn).visible){
var columnDG:DataGridColumn=(myGridObj.columns[j] as DataGridColumn);
if(columnDG.headerText.toLowerCase().indexOf(“date”)!=-1 ){ // if date text found in column, attach the common method
columnDG.sortCompareFunction=sortDate(columnDG.fieldName+””);  // pass the field name, so that on fly t will pick which field to sort

trace(“Error Message “:+e”);

private static function sortDate(fieldName:String):Function{
return function (obj1:Object, obj2:Object):int {
var datA:Date = new Date(Date.parse(obj1[fieldName]));
var datB:Date = new Date(Date.parse(obj2[fieldName]));
return ObjectUtil.dateCompare(datA, datB);


R Vashi

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.