1 Reply Latest reply on Dec 29, 2009 8:58 AM by Yozef0

    Sorting a column by date with Group Name set - I've tried EVERYTHING!!

    Yozef0 Level 1

      I have sorted my Array like so: (trace out below), the order key of each object is dynamically generated by the date (removed the "-"s), basically, I would LOVE to sort the date from past to present. This is what I have. This info populates an Advanced Data Drid. Though it put the rows Randomly... Though I know it can sort them, because if I click the header of the date colums... It sorts it. Frustrating!

       

       

       

      (Array)#0

        [0] (Object)#1

          date = "2009-12-07"

          joint_amount = "3604.33"

          joint_count = "266"

         order = "20091207"

          product_name = "site1"

        [1] (Object)#2

          date = "2009-12-07"

          joint_amount = "31499.73"

          joint_count = "1507"

          order = "20091207"

          product_name = "site2"

        [2] (Object)#3

          date = "2009-12-07"

          joint_amount = "362.35"

          joint_count = "13"

          order = "20091207"

          product_name = "site3"

        [3] (Object)#4

          date = "2009-12-07"

          joint_amount = "3376.04"

          joint_count = "437"

          order = "20091207"

          product_name = "site2Mobile"

        [4] (Object)#5

          date = "2009-12-07"

          joint_amount = "4824.67"

          joint_count = "238"

         order = "20091207"

          product_name = "site4"

        [5] (Object)#6

          date = "2009-12-07"

          joint_amount = "9785.98"

          joint_count = "398"

          order = "20091207"

          product_name = "site5"

        [6] (Object)#7

          date = "2009-12-07"

          joint_amount = "1175.49"

          joint_count = "62"

         order = "20091207"

          product_name = "site6"

        [7] (Object)#8

          date = "2009-12-07"

          joint_amount = "1016.62"

          joint_count = "30"

          order = "20091207"

          product_name = "site7"

        [8] (Object)#9

          date = "2009-12-07"

          joint_amount = "3696.69"

          joint_count = "885"

          order = "20091207"

          product_name = "site7 Whitelabels"

        [9] (Object)#10

          date = "2009-12-08"

          joint_amount = "30371.81"

          joint_count = "1489"

         order = "20091208"

          product_name = "site2"

        [10] (Object)#11

          date = "2009-12-08"

          joint_amount = "3016.36"

          joint_count = "417"

         order = "20091208"

          product_name = "site2Mobile"

        [11] (Object)#12

          date = "2009-12-08"

          joint_amount = "481.25"

          joint_count = "17"

          order = "20091208"

          product_name = "site7"

        [12] (Object)#13

          date = "2009-12-08"

          joint_amount = "3213.37"

          joint_count = "252"

          order = "20091208"

          product_name = "site1"

        [13] (Object)#14

          date = "2009-12-08"

          joint_amount = "1194.97"

          joint_count = "64"

         order = "20091208"

          product_name = "site6"

        [14] (Object)#15

          date = "2009-12-08"

          joint_amount = "5271.18"

          joint_count = "235"

         order = "20091208"

          product_name = "site4"

        [15] (Object)#16

          date = "2009-12-08"

          joint_amount = "9109.37"

          joint_count = "376"

          order = "20091208"

          product_name = "site5"

        [16] (Object)#17

          date = "2009-12-08"

          joint_amount = "3754.29"

          joint_count = "864"

         order = "20091208"

          product_name = "site7 Whitelabels"

        [17] (Object)#18

          date = "2009-12-08"

          joint_amount = "384.20"

          joint_count = "16"

          order = "20091208"

          product_name = "site3"

        [18] (Object)#19

          date = "2009-12-09"

          joint_amount = "799.20"

          joint_count = "308"

          order = "20091209"

          product_name = "site7 Whitelabels"

        [19] (Object)#20

          date = "2009-12-09"

          joint_amount = "1618.24"

          joint_count = "88"

         order = "20091209"

          product_name = "site1"

        [20] (Object)#21

          date = "2009-12-09"

          joint_amount = "40.79"

          joint_count = "8"

          order = "20091209"

          product_name = "site7"

        [21] (Object)#22

          date = "2009-12-09"

          joint_amount = "1363.20"

          joint_count = "188"

         order = "20091209"

          product_name = "site2Mobile"

        [22] (Object)#23

          date = "2009-12-09"

          joint_amount = "347.43"

          joint_count = "18"

          order = "20091209"

          product_name = "site6"

        [23] (Object)#24

          date = "2009-12-09"

          joint_amount = "2277.86"

          joint_count = "108"

         order = "20091209"

          product_name = "site5"

        [24] (Object)#25

          date = "2009-12-09"

          joint_amount = "7723.05"

          joint_count = "380"

         order = "20091209"

          product_name = "site2"

        [25] (Object)#26

          date = "2009-12-09"

          joint_amount = "1930.91"

          joint_count = "93"

          order = "20091209"

          product_name = "site4"

        [26] (Object)#27

          date = "2009-12-09"

          joint_amount = "164.30"

          joint_count = "14"

          order = "20091209"

          product_name = "site3"

       

      So I populate the GroupingCollection's source <mx:GroupingCollection id="gc" source="{newData}">

      The Grouping Field is <mx:GroupingField name="product_name">

      BUT - The Datagrid is Not Sorted by Date, even though, the array Above, is Sorted!

       

      I add another thing to the column I want sorted (the date)

      <mx:AdvancedDataGridColumn dataField="date" id="break_byCol" headerText="Date" sortCompareFunction="sortNumeric"/>

       

       

      private function sortNumeric(obj1:Object, obj2:Object):int {

          return ObjectUtil.numericCompare(obj1.order, obj2.order);

      }

       

       

      and still not sorted. Help please!

        • 1. Re: Sorting a column in an Advanced Datagrid by date, with Group Name
          Yozef0 Level 1

          The solution to this is:

           

          This took a while for me to solve. I wanted to group my rows by name and sorted by date for each group collection. Much thanks to Sameer Bhatt as he deserves the credit for finding this clean solution.


          So I have an ArrayCollection:

          [Bindable] private var siteTrafficData:Array = [ // dummy data
          {absoluteUniqueVisitors:"164630", avgPageViews:"2.5900", bounceRate:"29.4400", change:"0", date:"2009-12-07", id:"972900", name:"www.site1.com", newVisits:"51.9800", pageViews:"480718", timeOnSite:"216.4000", visitorsPerDay:"195716", visits:"158716"},
          {absoluteUniqueVisitors:"166841", avgPageViews:"2.5400", bounceRate:"29.0600", change:"1.85", date:"2009-12-09", id:"972940", name:"www.site1.com", newVisits:"52.0600", pageViews:"480913", timeOnSite:"211.3900", visitorsPerDay:"189674", visits:"189224"},
          {absoluteUniqueVisitors:"166841", avgPageViews:"2.5400", bounceRate:"29.0600", change:"1.85", date:"2009-12-05", id:"972960", name:"www.site1.com", newVisits:"52.0600", pageViews:"468544", timeOnSite:"216.4520", visitorsPerDay:"289224", visits:"219734"}];

           

           

          An Advanced DataGrid which groups the rows by the name.

           

          <mx:AdvancedDataGrid id="adv_dg" height="80%" width="80%" textAlign="center">
             <mx:dataProvider>
                <mx:GroupingCollection id="gc" source="{siteTrafficData}">
                   <mx:Grouping>
                      <mx:GroupingField name="name">
                         <mx:summaries>
                             <mx:SummaryRow summaryPlacement="group">
                                 <mx:fields>
                                     <mx:SummaryField dataField="visits" id="summaryVisits" label="visits" operation="{operation}"/>
                                     <mx:SummaryField dataField="change" id="summaryChange" label="change" operation="{operation}"/>
                                     <mx:SummaryField dataField="absoluteUniqueVisitors" id="summaryAbsUV" label="absoluteUniqueVisitors" operation="{operation}"/>
                                     <mx:SummaryField dataField="pageViews" id="summaryPageViews" label="pageViews" operation="{operation}"/>
                                     <mx:SummaryField dataField="avgPageViews" id="summaryAVGPageViews" label="avgPageViews" operation="AVG"/>
                                     <mx:SummaryField dataField="timeOnSite" id="timeOnSite" label="timeOnSite" operation="AVG"/>
                                     <mx:SummaryField dataField="bounceRate" id="summaryBounceRate" label="bounceRate" operation="AVG"/>
                                     <mx:SummaryField dataField="newVisits" id="summaryNewVisits" label="newVisits" operation="AVG"/>
                                     <mx:SummaryField dataField="visitorsPerDay" id="summaryVisPerDay" label="visitorsPerDay" operation="AVG"/>
                                </mx:fields>
                             </mx:SummaryRow>
                         </mx:summaries>
                      </mx:GroupingField>
                  </mx:Grouping>
                </mx:GroupingCollection>
             </mx:dataProvider>
               <mx:columns>
                <mx:AdvancedDataGridColumn dataField="date" headerText="Date"/>
                <mx:AdvancedDataGridColumn dataField="visits" headerText="Visits #"/>
                <mx:AdvancedDataGridColumn dataField="change" headerText="% Change" fontWeight="bold"/>
                <mx:AdvancedDataGridColumn dataField="absoluteUniqueVisitors" headerText="Abs. Unique Visitors"/>
                <mx:AdvancedDataGridColumn dataField="pageViews" headerText="Page Views" />
                <mx:AdvancedDataGridColumn dataField="avgPageViews" headerText="Avg. Page Views" />
                <mx:AdvancedDataGridColumn dataField="timeOnSite" headerText="Time on Site"/>
                <mx:AdvancedDataGridColumn dataField="bounceRate" headerText="Bounce Rate"/>
                <mx:AdvancedDataGridColumn dataField="newVisits" headerText="New Visits"/>
                <mx:AdvancedDataGridColumn dataField="visitorsPerDay" headerText="Visitors Per Day"/>
               </mx:columns>
          </mx:AdvancedDataGrid>

           

          Now for the Sorting by date – the Magic:

           

          private function populateGrid():void {
             gc.source = siteTrafficData;
             gc.refresh(); 
          
             adv_dg.validateNow();
          
             var sort:Sort = new Sort();
                 sort.fields = [new SortField("date")];
             IHierarchicalCollectionView(adv_dg.dataProvider).sort = sort;
             IHierarchicalCollectionView(adv_dg.dataProvider).refresh();
          }