Skip to content

Sorting ArrayCollection by date: YYYY-MM-DD

December 9, 2009

Many posts online try to tackle this… They didn’t work for me, but you can understand this snippet (thanks to Michael)

I have an ArrayCollection of Objects. Each Object has the following keys/values:

var collection:ArrayCollection = new ArrayCollection([{date: 2009-12-01, visits:13555, bounceRate:45},
                                                      {date: 2009-12-05, visits:46955, bounceRate:45},
                                                      {date: 2009-12-06, visits:13685, bounceRate:45},
                                                      {date: 2009-12-02, visits:13685, bounceRate:45},
                                                      {date: 2009-12-04, visits:68755, bounceRate:45},
                                                      {date: 2009-12-03, visits:35875, bounceRate:45}]);

I need to sort this ArrayCollection by date, so it would be from past to present.


private function sortByDate():void {
     for (var i:int = 0; i < collection.length; ++i) {
          collection[i].formattedDate = getActualDate(collection[i].date);
          collection[i].dateTime = collection[i].formattedDate.time;
          trace(collection[i].dateTime);
     }
     var sort:Sort = new Sort();
         sort.fields = [new SortField("dateTime", false, false, true)];

     collection.sort = sort;
     collection.refresh();

     // just to verify
     for (var i:int = 0; i < collection.length; ++i) {
          trace(collection[i].dateTime);
     }
}

private function getActualDate(value:String):Date {
     var values:Array = value.split("-");
     return new Date(int(values[0]), int(values[1]) - 1, int(values[2]));
}

Advertisements

From → AIR, Uncategorized

One Comment
  1. t get too much direct heat during the cooking
    process. Just be sure to sprinkle the bottom with cornmeal so the pizza
    will slide easier and pull the oven rack out
    so you aren’t likely to get injured while sliding it onto the pizza stone. You have to shake over the paddle of the openhandedly by taking use of the corn meal & then you have to put bread on the paddle ahead of mixing toppings within it.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: