6 Replies Latest reply on May 5, 2011 5:43 AM by ssivabharathi@gmail.com

    Sorting date Field

    ssivabharathi@gmail.com Level 1

      Hi,

            I am using  groupField in Advanced Datagird, in that i want sort the date field. The  date field sees like that 06/22/1986(Month/Date/Year). How i can sort  it.

       

       

      Regards,

      Sivabharathi

        • 1. Re: Sorting date Field
          Anonymous2008

          (Sorry for the bad formatting, text only mail conversion)

           

          There are a few ways off the top of my head to modify the sort behavior.

           

          1.  If the data is string based date, change the format to be YYYY/MM/DD.  This will allow it to string sort it.

           

          2.  The DataGridColumn has a sortCompareFunction you may create your own compare function using the ObjectUtil dateCompare function which returns similar values required by the

           

          http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/dataGridCla sses/DataGridColumn.html#sortCompareFunction

           

          mySortCompareFunction(obj1:Object, obj2:Object):int

          The function should return a value based on the comparison of the objects:

          •     -1 if obj1 should appear before obj2 in ascending order.

          •     0 if obj1 = obj2.

          •     1 if obj1 should appear after obj2 in ascending order.

          http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/ObjectUtil.htm l#dateCompare()

          dateCompare(a:Date, b:Date):int

           

          3. Add sorting to the dataProvider such as an XMLListCollection or an ArrayCollection.  They have both filterFunction and a sort function support.

          http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/ListColl ectionView.html#sort

          Setting the sort does not automatically refresh the view, so you must call the refresh() method after setting this property

          http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/Sort.htm l

          Using the mx.collections.sort

           

           

          Hope this helps a little

          • 2. Re: Sorting date Field
            ssivabharathi@gmail.com Level 1

            Hi,

             

            Thank you for your reply. I tried sortComparefunction. But it's not working perfect. Look at the example.

             

             

            <?xml version="1.0" encoding="utf-8"?>
            <!-- http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/ -->
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
               
                <mx:Script>
                    <![CDATA[
                        import mx.collections.XMLListCollection;
                        import mx.events.FlexEvent;
                        import mx.utils.ObjectUtil;
                       
                        public var reportXMLList:XMLList = new XMLList();
                        public var listCollection:XMLListCollection ;
                        private var tempXML:XML ;
                       
                        private function date_sortCompareFunc(itemA:Object, itemB:Object):int {
                            var dateA:Date = new Date(Date.parse(itemA.dob));
                            var dateB:Date = new Date(Date.parse(itemB.dob));
                            return ObjectUtil.dateCompare(dateA, dateB);
                        }

             

                        protected function init():void
                        {
                            tempXML = new XML(<root>
                                            <row col1="12/27/2010"/>
                                            <row col1="12/29/2010"/>
                                            <row col1="01/05/2011"/>
                                            <row col1="01/07/2011"/>
                                            <row col1="01/31/2011"/>
                                            <row col1="02/15/2011"/>
                                            <row col1="02/17/2011"/>
                                            <row col1="02/18/2011"/>
                                            <row col1="02/25/2011"/>
                                            <row col1="03/11/2011"/>
                                            <row col1="03/18/2011"/>
                                            <row col1="03/19/2011"/>
                                            <row col1="03/21/2011"/>
                                            <row col1="03/22/2011"/>
                                            <row col1="03/26/2011"/>
                                            <row col1="03/30/2011"/>
                                            <row col1="03/30/2010"/>
                                            <row col1="03/30/2009"/>
                                            <row col1="03/30/2015"/>
                                          </root>);
                            reportXMLList = tempXML.row;
                            listCollection= new XMLListCollection(reportXMLList);
                        }

             

                    ]]>
                </mx:Script>
               
                <mx:AdvancedDataGrid id="dataGrid" initialize="init()" dataProvider="{listCollection}"
                                     width="100%" height="100%">
                    <mx:groupedColumns>
                        <mx:AdvancedDataGridColumn dataField="@col1"
                                           headerText="Date of birth:"
                                           sortCompareFunction="date_sortCompareFunc"
                                           />
                    </mx:groupedColumns>
                </mx:AdvancedDataGrid>
               
            </mx:Application>

            • 3. Re: Sorting date Field
              Anonymous2008 Level 1

              Ok I found a couple of corrections for you.  I tried to keep everything formatted the same.

               

              Added a binding to the dataProvider.

               

                          [Bindable]
                          public var listCollection:XMLListCollection ;

               

              And your sort function seems to be trying to load a child node called "dob" that doesn't exist.  It needs to load your property for the row node called "@col1".

               

                          private function date_sortCompareFunc(itemA:Object, itemB:Object):int

                          {
                              var dateA:Date = new Date(Date.parse(itemA.@col1));

                              var dateB:Date = new Date(Date.parse(itemB.@col1));
                              return ObjectUtil.dateCompare(dateA, dateB);
                          }

               

               

              Seems to work now.  On a side note, an advanced datagrid seems to add about 100k more compiled than a regular datagrid.

              • 4. Re: Sorting date Field
                ssivabharathi@gmail.com Level 1

                Hi,

                 

                  Thank for your reply. It's really helps for me.

                 

                Here your given itemA.@col1 as static. But i my report date field will come any place like

                 

                1) <row col1="01/05/2011" col2="a"/>

                 

                2) <row col1="b" col2="01/05/2011" />

                 

                3) <row col1="c" col2="2" col3="01/05/2011" />

                 

                Because my report is not static column. It's dynamic, so date field will come any place (col1, col2 or col3). Then how i can give the itemA.@col1.

                 

                private function date_sortCompareFunc(itemA:Object, itemB:Object):int

                            {
                                var dateA:Date = new Date(Date.parse(itemA.@col1));

                                var dateB:Date = new Date(Date.parse(itemB.@col1));
                                return ObjectUtil.dateCompare(dateA, dateB);
                            }

                 

                 

                Sorry, I cant understand your note.(An advanced datagrid seems to add about 100k more compiled than a regular datagrid.)

                 

                Regards,

                Sivabharathi.

                • 5. Re: Sorting date Field
                  Anonymous2008 Level 1

                  I believe each DataGridColumn uses its own sortCompareFunction.  I see it does not allow additional values to be passed other than the two row objects.  You may need to create a new sortCompareFunction for each column.  Would that fix the column naming maybe?

                   

                  For the note, the AdvancedDataGrid is a larger component.  It has a few extra features such as grouping.  But when I looked at the swf file size difference between the advanced and the regular it was 100k file size difference for me.

                  • 6. Re: Sorting date Field
                    ssivabharathi@gmail.com Level 1

                    Hi,

                     

                      Thank you for your reply. If any other way there to assign values like this.

                     

                    Var value1:String = new String("col1');

                    Var value2:String = new String("col1');

                     

                      private function date_sortCompareFunc(itemA:Object, itemB:Object):int

                                {
                                    var dateA:Date = new Date(Date.parse(itemA.@+"value1"));

                                    var dateB:Date = new Date(Date.parse(itemB.@+"value2"));
                                    return ObjectUtil.dateCompare(dateA, dateB);
                                }

                     

                     

                    It's not a possible each to create one method for each column. Because my report is dynamic, the column will increase or decrease. So i cant recreate method for all the columns. I want one method it's supports all the columns. How i can do this.

                     

                     

                    Regards,

                    Sivabharathi