Sorting Dates In a DataGrid In Flex 2

  When I googled “sorting dates in a datagrid in flex” I found all sorts of outdated material, and some really complicated results, that i tried, but they just didnt happen to work in my situation… i’m not sure what everyone else was doing to create their dates, but I’ve heard a number of people saying it was very hard, or at least harder than they imagined…

APPLICATION   – SOURCE

that it is… a true statement in deed… just a note, but i havent even tried filtering on this… so forgive me if my solution wont work for it…

I kept battling this issue, and went back to one of the books i read “Adobe Flex 2 training from the source” and found an example in there, and built from it…

/////////////////////script block/////////// private function dateLabel(item:Object, column:DataGridColumn):String

{

return dateFormatter.format(item[column.dataField]);

}

/////////////////////script block///////////

<mx:DateFormatter id=“dateFormatter”/>

<mx:DataGrid dataProvider=“{acData}”>

<mx:columns>

<mx:DataGridColumn dataField=“name” />

<mx:DataGridColumn dataField=“dob”

labelFunction=“dateLabel”/>

</mx:columns>

</mx:DataGrid>

Please refer to the source links at the top and bottom of the entry for the real source… that code block is not safe to copy and paste and just work…. its just psuedocode.

If you notice I use a mx:DateFormatter id=”dateFormatter” and by default the format is ‘mm/dd/yyyy’ so we dont need to supply a format for this example…

and in the script block there is a very simple function that just formats the label of the date column… and returns a string… and my sorting is working fine for now… please feel free to comment if you see any downfalls in what I have done…

APPLICATION   – SOURCE

5 comments on this post.
  1. andhapp:

    Your code works like a charm….

  2. nw:

    Very nice!

    Your use of item[column.dataField] was exactly what I needed to know.

  3. Ricardo:

    Great solution. Clean and works very well.
    Thanks for the code.

  4. Gio:

    This won’t work if you include a format string, as in the following

  5. Gio:

    continue for above comment
    formatString=”MM/DD/YYYY”

Leave a comment