9 Replies Latest reply on Mar 23, 2011 5:51 AM by crumsberry

    How do I sort this time ArrayCollection?

    crumsberry Level 1

      How do I sort this time ArrayCollection? I need it to be sorted from earliest to latest.

      "8:00am"
      "2:00pm"
      "10:00am"
      "11:00am"
      "12:00pm"
      "7:00am"
      "3:00pm"
      "4:00pm"

        • 1. Re: How do I sort this time ArrayCollection?
          markerline Level 4

          I'm not sure if this would apply.  This is how I would do it in ActionScript 3.0 .

          Make a duplex array:

           

          var dupArray:Array=new Array([7,7],[8,8],[10,10],[11,11],[12,12],[3,15],[4,16]).

           

          Obviously you gave this only as an example and are dealing with a much larger array.  You could dynamically push elements into the Array as duplex items, I believe, and then use a sort() method.  The reason I gave military time is that this will arrange your duplex from 0 to 23 and any PM numbers will follow suit.  You just make the proper if statement to set the condition that if (dupArray[6][1]){ dupArray[6][0]} or something to that effect.

          • 2. Re: How do I sort this time ArrayCollection?
            crumsberry Level 1

            I can't use military time. I was hoping to use something like sort by numbers then sort that array by am, pm. and add the array back together or something.

            • 3. Re: How do I sort this time ArrayCollection?
              markerline Level 4

              That solution would work.

               

              One thing.  Do you have times like 3:30pm or 4:15pm?  You will have to think of your time in incremental values creating conditions to break up the time fields.  Have a condition for minutes and a separate condition for hours and nest them.

              • 4. Re: How do I sort this time ArrayCollection?
                crumsberry Level 1

                Yes I do have those times. However I am not sure if I am experienced enough to write this algorith.

                • 5. Re: How do I sort this time ArrayCollection?
                  markerline Level 4

                  Give me a little while I will try something in Flash.

                  • 6. Re: How do I sort this time ArrayCollection?
                    _spoboyle Level 4
                    <?xml version="1.0" encoding="utf-8"?>
                    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                      xmlns:s="library://ns.adobe.com/flex/spark" 
                                      xmlns:mx="library://ns.adobe.com/flex/mx"
                                      minWidth="955" minHeight="600" xmlns:local="*"
                                      creationComplete="init()">
                         <fx:Script>
                              <![CDATA[
                                   import mx.collections.ArrayCollection;
                                   import mx.collections.Sort;
                                   
                                   private var data:ArrayCollection = new ArrayCollection([
                                        "8:01am",
                                        "8:00am",
                                        "2:00pm",
                                        "10:00am",
                                        "11:00am",
                                        "12:00pm",
                                        "7:00am",
                                        "3:00pm",
                                        "4:00pm",
                                   ]);
                                   
                                   private function init():void
                                   {
                                        var sort:Sort = new Sort();
                                        sort.compareFunction = compareTime;
                                        data.sort = sort;
                                        data.refresh();
                                        
                                        trace("data: " + data);
                                   }
                                   
                                   private function compareTime(value1:String, value2:String, fields:Array = null):int
                                   {
                                        var meridian1:String = value1.substring(value1.length-2);
                                        var meridian2:String = value2.substring(value2.length-2);
                                        var hour1:int = parseInt(value1.substring(0, value1.indexOf(":")));
                                        var hour2:int = parseInt(value2.substring(0, value2.indexOf(":")));
                                        var minutes1:int = parseInt(value1.substring(value1.indexOf(":")+1, value1.length-2));
                                        var minutes2:int = parseInt(value2.substring(value2.indexOf(":")+1, value2.length-2));
                                        
                                        if (meridian1 < meridian2)
                                        {
                                             return -1;
                                        }
                                        else if (meridian1 > meridian2)
                                        {
                                             return 1;
                                        }
                                        else
                                        {
                                             if (hour1 < hour2)
                                             {
                                                  return -1;
                                             }
                                             else if (hour1 > hour2)
                                             {
                                                  return 1;
                                             }
                                             else
                                             {
                                                  if (minutes1 < minutes2)
                                                  {
                                                       return -1;
                                                  }
                                                  else if (minutes1 > minutes2)
                                                  {
                                                       return 1;
                                                  }
                                                  else
                                                  {
                                                       return 0;
                                                  }
                                             }
                                        }
                                   }
                                   
                              ]]>
                         </fx:Script>
                    </s:Application>
                    
                    
                    1 person found this helpful
                    • 7. Re: How do I sort this time ArrayCollection?
                      markerline Level 4

                      Have a look at this example.  the one I was working on I can't seem to apply logic to.

                       

                      http://polygeek.com/144_flex_sort-an-array-of-objects-by-date

                       

                      also look at the Date class:

                       

                      http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html

                       

                      If you can rewrite your times in terms of the Date class then you will be able to apply the logic in the first code to sort your times.

                       

                      The Date class writes times as follows:

                       

                      var someBirthday:Date = new Date(1974, 10, 30, 1, 20);

                      trace(someBirthday);           // Sat Nov 30 01:20:00 GMT-0800 1974

                      trace(someBirthday.getFullYear()); // 1974

                       

                      Message was edited by: markerline

                      1 person found this helpful
                      • 8. Re: How do I sort this time ArrayCollection?
                        crumsberry Level 1

                        I see where your going with this.

                        I had to make a special exeption for noon and midnight, because I us am and pm for increased value.

                        I have access to the variables before they enter the array. However its not sorting by the value, its sorting by number, kinda. For example 100010 is above 70010 when it should be below it. It should go, 70010, 80010, 90010, 100010. Because its a greater number.

                         

                        [Bindable]

                        public var mondayTimeArray:ArrayCollection = new ArrayCollection();

                         

                        public function mondayAddTime():void

                        {

                        sortN = new String();

                         

                        if(mondayTimeEntry.hour == 12 && mondayTimeEntry.am_pm == "am"){

                        sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute);

                         

                        mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                         

                        var sort1:Sort = new Sort();

                        sort1.fields = [new SortField('sorter', true)];

                        mondayTimeArray.sort = sort1;

                        mondayTimeArray.refresh();

                         

                        return;

                        }

                        else if(mondayTimeEntry.hour == 12 && mondayTimeEntry.am_pm == "pm"){

                        sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute) + 10;

                         

                        mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                         

                        var sort2:Sort = new Sort();

                        sort2.fields = [new SortField('sorter', true)];

                        mondayTimeArray.sort = sort2;

                        mondayTimeArray.refresh();

                         

                        return;

                        }

                        if(mondayTimeEntry.am_pm == "am"){

                        ampm = "10";

                        }

                        else{

                        ampm = "1000";

                        }

                        sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute) + ampm;

                        mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                         

                        var sort:Sort = new Sort();

                        sort.fields = [new SortField('sorter', true)];

                        mondayTimeArray.sort = sort;

                        mondayTimeArray.refresh();

                        }

                        • 9. Re: How do I sort this time ArrayCollection?
                          crumsberry Level 1

                          Ok I finaly got it.

                           

                          [Bindable]

                          public var mondayTimeArray:ArrayCollection = new ArrayCollection();

                           

                          public function mondayAddTime():void

                          {

                          sortN = new String();

                           

                          if(mondayTimeEntry.hour == 12 && mondayTimeEntry.am_pm == "am"){

                          sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute);

                           

                          mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                           

                          var sort1:Sort = new Sort();

                          var dataSortField1:SortField = new SortField();

                          dataSortField1.name = "sorter";

                          dataSortField1.numeric = true;

                          sort1.fields = [dataSortField1];

                          mondayTimeArray.sort = sort1;

                          mondayTimeArray.refresh();

                           

                          return;

                          }

                          else if(mondayTimeEntry.hour == 12 && mondayTimeEntry.am_pm == "pm"){

                          sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute) + 10;

                           

                          mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                           

                          var sort2:Sort = new Sort();

                          var dataSortField2:SortField = new SortField();

                          dataSortField2.name = "sorter";

                          dataSortField2.numeric = true;

                          sort2.fields = [dataSortField2];

                          mondayTimeArray.sort = sort2;

                          mondayTimeArray.refresh();

                           

                          return;

                          }

                          if(mondayTimeEntry.am_pm == "am"){

                          ampm = "10";

                          }

                          else{

                          ampm = "1000";

                          }

                          sortN = mondayTimeEntry.hour + formatValue(mondayTimeEntry.minute) + ampm;

                          mondayTimeArray.addItem({sorter:sortN, time:mondayTimeEntry.hour + ':' + formatValue(mondayTimeEntry.minute) + mondayTimeEntry.am_pm});

                           

                          var sort:Sort = new Sort();

                          var dataSortField:SortField = new SortField();

                          dataSortField.name = "sorter";

                          dataSortField.numeric = true;

                          sort.fields = [dataSortField];

                          mondayTimeArray.sort = sort;

                          mondayTimeArray.refresh();

                          }