7 Replies Latest reply on Dec 8, 2009 9:20 AM by Yozef0

    Sorting ArrayCollection by date: YYYY-MM-DD

    Yozef0 Level 1

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

       

      {date: 2009-12-01, visits=13555, bouceRate=45}

      {date: 2009-12-05, visits=46955, bouceRate=45}

      {date: 2009-12-06, visits=13685, bouceRate=45}

      {date: 2009-12-02, visits=13685, bouceRate=45}

      {date: 2009-12-04, visits=68755, bouceRate=45}

      {date: 2009-12-03, visits=35875, bouceRate=45}

      .

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

       

      {date: 2009-12-01, visits=13555, bouceRate=45}

      {date: 2009-12-02, visits=13685, bouceRate=45}

      {date: 2009-12-03, visits=35875, bouceRate=45}

      {date: 2009-12-04, visits=68755, bouceRate=45}

      {date: 2009-12-05, visits=46955, bouceRate=45}

      {date: 2009-12-06, visits=13685, bouceRate=45}

        • 1. Re: Sorting ArrayCollection by date: YYYY-MM-DD
          Michael Hovey Level 1

          There's probably a better way, but what I usually do is start by converting your YYYY-MM-DD date to an actual AS3 date object and then you can reference the time property on the date, which is the number of milliseconds since Jan 1, 1970. Here's an example:

           

          public 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}]);
          
          private function sortByDate():void
          {
               var i:int;
               
               for(i = 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
               trace("");
               
               for(i = 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]));
          }
          

           

          Might not be the best/most efficient approach, but it works. Let me know what you think.

          • 2. Re: Sorting ArrayCollection by date: YYYY-MM-DD
            Yozef0 Level 1

            Hi Michael...

             

            Here is what I used from your code... and it would be kind if you pointed to me my mistake... I'll show you my code... and the trace out.

             

            var dateSort:Sort = new Sort();

                dateSort.fields = [new SortField("date", false, false, true)];

             

            newAreaChartData.sort = dateSort;     // newAreaChartData is the ArrayCollection in question

            // traceout

            for (var i:int = 0; i <newAreaChartData.length; i++)

                trace ("Object #" + i + ": " + ObjectUtil.toString(newAreaChartData.getItemAt(i)));

             

             

            and the trace out is:

            Object #0: (Object)#0

              date = "2009-12-02"

              site1.com = "1904233"

              site2.com = "396159"

              site3.com = "766748"

              site4.com = "195410"

              site5.com = "3415356"

              site6.com = "125543"

              site7.com = "13289904"

              site8.com = "8139380"

            Object #1: (Object)#0

              date = "2009-12-05"

              site1.com = "1780687"

              site2.com = "376472"

              site3.com = "535846"

              site4.com = "188932"

              site5.com = "3473486"

              site6.com = "117201"

              site7.com = "12289240"

              site8.com = "8055336"

            Object #2: (Object)#0

              date = "2009-11-29"

              site1.com = "2035242"

              site2.com = "371729"

              site3.com = "753506"

              site4.com = "179708"

              site5.com = "3276611"

              site6.com = "119088"

              site7.com = "12456564"

              site8.com = "7972426"

            Object #3: (Object)#0

              date = "2009-11-30"

              site1.com = "1910690"

              site2.com = "395665"

              site3.com = "817851"

              site4.com = "188802"

              site5.com = "3502506"

              site6.com = "126520"

              site7.com = "13532128"

              site8.com = "8566676"

            Object #4: (Object)#0

              date = "2009-12-06"

              site1.com = "2034830"

              site2.com = "380447"

              site3.com = "515144"

              site4.com = "181447"

              site5.com = "3430137"

              site6.com = "113508"

              site7.com = "12318165"

              site8.com = "8047809"

            Object #5: (Object)#0

              date = "2009-11-28"

              site1.com = "1738892"

              site2.com = "371333"

              site3.com = "720097"

              site4.com = "178153"

              site5.com = "3203534"

              site6.com = "118193"

              site7.com = "12002075"

              site8.com = "7687377"

            Object #6: (Object)#0

              date = "2009-12-04"

              site1.com = "1682202"

              site2.com = "380162"

              site3.com = "524501"

              site4.com = "193161"

              site5.com = "3511696"

              site6.com = "120209"

              site7.com = "12891923"

              site8.com = "8075326"

            Object #7: (Object)#0

              date = "2009-12-07"

              site1.com = "1945027"

              site2.com = "400709"

              site3.com = "539380"

              site4.com = "189254"

              site5.com = "3747369"

              site6.com = "122667"

              site7.com = "13204022"

              site8.com = "8650371"

            Object #8: (Object)#0

              date = "2009-12-03"

              site1.com = "1851853"

              site2.com = "393393"

              site3.com = "691452"

              site4.com = "195428"

              site5.com = "3502571"

              site6.com = "121243"

              site7.com = "13170420"

              site8.com = "8046791"

            Object #9: (Object)#0

              date = "2009-12-01"

              site1.com = "1890569"

              site2.com = "394063"

              site3.com = "751967"

              site4.com = "188002"

              site5.com = "3404434"

              site6.com = "125635"

              site7.com = "13222986"

              site8.com = "8256714"

            • 3. Re: Sorting ArrayCollection by date: YYYY-MM-DD
              Michael Hovey Level 1

              From what I can tell, you aren't executing the first part of my code that actually creates an Actionscript 3 Date object from your date string (YYYY-MM-DD). This is necessary because then you can reference the time property of the Date class and do a basic numeric sort on that. Take a look at this function:

               

              for(i = 0; i < collection.length; ++i)
                   {
                        collection[i].formattedDate = getActualDate(collection[i].date);
                        collection[i].dateTime = collection[i].formattedDate.time;
                       
                        trace(collection[i].dateTime);
                   }
              • 4. Re: Sorting ArrayCollection by date: YYYY-MM-DD
                Yozef0 Level 1

                Thanks for the reply Michael

                 

                Updated code:

                 

                 

                for(var i:int = 0; i < newAreaChartData.length; ++i) {

                         newAreaChartData[i].formattedDate = getActualDate(newAreaChartData[i].date);

                         newAreaChartData[i].date = newAreaChartData[i].formattedDate.date;

                        

                         trace("newAreaChart[i].dateTime: " + newAreaChartData[i].date);

                    }

                    

                var dateSort:Sort = new Sort();

                    dateSort.fields = [new SortField("date", false, false, true)];

                    newAreaChartData.sort = dateSort;

                    newAreaChartData.refresh();

                   

                    for (var i:int = 0; i <newAreaChartData.length; i++)

                    trace ("Object #"+ i + ": " + ObjectUtil.toString(newAreaChartData.getItemAt(i)));

                   

                 

                private function getActualDate(value:String):Date {

                     var values:Array = value.split("-");

                     return new Date(int(values[0]), int(values[1]) - 1, int(values[2]));

                }

                 

                 

                This actually does Sort it but, I lost my date key to a numeric value, and the formatted date has changed from YYYY-MM-DD

                 

                 

                newAreaChart[i].date: 29

                newAreaChart[i].date: 6

                newAreaChart[i].date: 4

                newAreaChart[i].date: 30

                newAreaChart[i].date: 2

                newAreaChart[i].date: 1

                newAreaChart[i].date: 3

                newAreaChart[i].date: 7

                newAreaChart[i].date: 28

                newAreaChart[i].date: 5

                 

                 

                Object #0: (Object)#0

                  date = 1

                  formattedDate = Tue Dec 1 00:00:00 GMT-0500 2009

                  site1.com = "1890569"

                  site2.com = "394063"

                  site3.com = "751967"

                  site4.com = "188002"

                  site5.com = "3404434"

                  site6.com = "125635"

                  site7.com = "13222986"

                  site8.com = "8256714"

                Object #1: (Object)#0

                  date = 2

                  formattedDate = Wed Dec 2 00:00:00 GMT-0500 2009

                  site1.com = "1904233"

                  site2.com = "396159"

                  site3.com = "766748"

                  site4.com = "195410"

                  site5.com = "3415356"

                  site6.com = "125543"

                  site7.com = "13289904"

                  site8.com = "8139380"

                Object #2: (Object)#0

                  date = 3

                  formattedDate = Thu Dec 3 00:00:00 GMT-0500 2009

                  site1.com = "1851853"

                  site2.com = "393393"

                  site3.com = "691452"

                  site4.com = "195428"

                  site5.com = "3502571"

                  site6.com = "121243"

                  site7.com = "13170420"

                  site8.com = "8046791"

                Object #3: (Object)#0

                  date = 4

                  formattedDate = Fri Dec 4 00:00:00 GMT-0500 2009

                  site1.com = "1682202"

                  site2.com = "380162"

                  site3.com = "524501"

                  site4.com = "193161"

                  site5.com = "3511696"

                  site6.com = "120209"

                  site7.com = "12891923"

                  site8.com = "8075326"

                Object #4: (Object)#0

                  date = 5

                  formattedDate = Sat Dec 5 00:00:00 GMT-0500 2009

                  site1.com = "1780687"

                  site2.com = "376472"

                  site3.com = "535846"

                  site4.com = "188932"

                  site5.com = "3473486"

                  site6.com = "117201"

                  site7.com = "12289240"

                  site8.com = "8055336"

                Object #5: (Object)#0

                  date = 6

                  formattedDate = Sun Dec 6 00:00:00 GMT-0500 2009

                  site1.com = "2034830"

                  site2.com = "380447"

                  site3.com = "515144"

                  site4.com = "181447"

                  site5.com = "3430137"

                  site6.com = "113508"

                  site7.com = "12318165"

                  site8.com = "8047809"

                Object #6: (Object)#0

                  date = 7

                  formattedDate = Mon Dec 7 00:00:00 GMT-0500 2009

                  site1.com = "1945027"

                  site2.com = "400709"

                  site3.com = "539380"

                  site4.com = "189254"

                  site5.com = "3747369"

                  site6.com = "122667"

                  site7.com = "13204022"

                  site8.com = "8650371"

                Object #7: (Object)#0

                  date = 28

                  formattedDate = Sat Nov 28 00:00:00 GMT-0500 2009

                  site1.com = "1738892"

                  site2.com = "371333"

                  site3.com = "720097"

                  site4.com = "178153"

                  site5.com = "3203534"

                  site6.com = "118193"

                  site7.com = "12002075"

                  site8.com = "7687377"

                Object #8: (Object)#0

                  date = 29

                  formattedDate = Sun Nov 29 00:00:00 GMT-0500 2009

                  site1.com = "2035242"

                  site2.com = "371729"

                  site3.com = "753506"

                  site4.com = "179708"

                  site5.com = "3276611"

                  site6.com = "119088"

                  site7.com = "12456564"

                  site8.com = "7972426"

                Object #9: (Object)#0

                  date = 30

                  formattedDate = Mon Nov 30 00:00:00 GMT-0500 2009

                  site1.com = "1910690"

                  site2.com = "395665"

                  site3.com = "817851"

                  site4.com = "188802"

                  site5.com = "3502506"

                  site6.com = "126520"

                  site7.com = "13532128"

                  site8.com = "8566676"

                • 5. Re: Sorting ArrayCollection by date: YYYY-MM-DD
                  Michael Hovey Level 1

                  This is because you overwrote your existing date property with the Date.date value, which is the current date of the month (1 - 31). There's no need to overwrite your YYYY-MM-DD value so just keep that, and instead, use formattedDate for the AS3 Date Object which you are, and then use a property called dateTime to store the value of formattedDate.time. Then, the property you want to sort on is "dateTime".

                  • 7. Re: Sorting ArrayCollection by date: YYYY-MM-DD
                    Yozef0 Level 1

                    Michael... THANK YOU!!!!

                     

                    I'll actually place this on my blog ( flexr.wordpress.com ) ... I saw many solutions on this specific problem... none gave me a concrete solution... this one will do for now. Thanks again.