Sorting Date’s on ArrayCollection in Flex

First things first, read about the mx.collections.Sort Class before going in to the code below.

About the Post,suppose i have a AC[ArrayCollection] like below;

private var arrClldetails:ArrayCollection = new ArrayCollection([
{Fname:”Kranthi”, Lname:”Kata”, dob:”21 Sep 79″},
{Fname:”Vasanth”, Lname:”Lola”, dob:”11 Jun 80″},
{Fname:”Manoj”, Lname:”Pati”, dob:”16 July 81″},
{Fname:”John McClain”, Lname:”Mela”, dob:”15 Feb 72″},
{Fname:”Ross”, Lname:”Geller”, dob:”02 Jan 74″},
{Fname:”Chandler”, Lname:”Bing”, dob:”18 Oct 76″}

Now i want to Sort the “dob” Date field in the AC.,so for this i need to
write a compareFunction for Sort which in turn is applied to the AC like below,


//Compare Function
private function fnCompareFunction(ObjA:Object,ObjB:Object,fields:Array = null):int
//fnDtfParceFunct is a Date parse function avail in DownloadCode.
var dateA:Date=fnDtfParceFunct(ObjA.dob,”DD/MM/YY”);
var dateB:Date=fnDtfParceFunct(ObjB.dob,”DD/MM/YY”);
return ObjectUtil.dateCompare(dateA, dateB);

Now apply this CompareFunction to Sort and then apply that to AC like below,

var sort:Sort=new Sort();

Now refresh the AC for Sort to take effect,


Run Demo

Download Code

Enjoy the post.


13 Responses to Sorting Date’s on ArrayCollection in Flex

  1. Jloa says:

    The sort function doesn’t sort data properly. You should convert date stamps in your fnDtfParceFunct() to int Numbers and compare them like numeric values.

    • kumargandhi says:

      It does work correctly,let me explain,suppose i have Date in DD MMM YY [24 Nov 09] format, so when i parse this to get my Date object, i get something like this “Tue Nov 24 00:00:00 GMT+0530 2009″, now you can observe the Timestamp its 0,well all the dates in the example have 0 timestamps,and please explore ObjectUtil.dateCompare(),it does what you have mentioned.


  2. OMG enjoyed reading this article. I submitted your feed to my reader.

  3. Ajay says:

    Above will sort descending order wright??

    Suppose if i want to sort ascending order??

  4. slowcooked says:

    Thank’s for the post Kumar, it put me on the correct path. To answer Ajay’s question about sort order: Simply swap dateA and dateB in the call to dateCompare(),

    return ObjectUtil.dateCompare(dateA.dateB)// ASC
    return ObjectUtil.dateCompare(dateB,dateA)// DESC

    • kumargandhi says:


      your welcome, and thanks for sharing other info on the blog, but for that above question one could figure it out if he/she has seen the code written on ObjectUtil class and its method dateCompare, its simple logic though, but anyways thanks for sharing.


  5. felix says:

    an alternative is to convert your strings to date objects and use the date.time with a simple numerical sort

  6. VikramAdith says:

    Very useful! Thanks 🙂

  7. Vijay says:

    awesome… gr8 works

  8. Camaleon says:

    Thanks for the info, I also have another issue: how would be if I need to sort 3 fields, two of them date fields?

    I used to have something like:

    sortArray = new Sort();

    sortArray.fields = [new SortField(“id”, false, false),
    new SortField(“name”, true, false)];

    gridAC.sort = sortArray;

    but is not working with dates 😦


  9. Farhad says:

    Hi Kumar, I’m trying to use your code in my application, but I get an error in the following line saying “Access of undefined property fnCompareFunction”

    sort.compareFunction = fnCompareFunction;

    although I defined the fnCompareFunction function. Could you please give me an idea how I can resolve the issue?

  10. Manu Mohan says:

    Good one, the exact thing i needed, Thanks!!..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: