Skip to main content
Participant
April 22, 2011
Question

Sorting date Field

  • April 22, 2011
  • 1 reply
  • 2844 views

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

This topic has been closed for replies.

1 reply

Participating Frequently
April 25, 2011

(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/dataGridClasses/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.html#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/ListCollectionView.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.html

Using the mx.collections.sort

Hope this helps a little

Participant
May 2, 2011

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>

Participating Frequently
May 2, 2011

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.