<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Sorting date columns in a DataGrid</title>
	<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/</link>
	<description>A bunch of examples for Adobe Flex and ActionScript</description>
	<pubDate>Fri, 05 Dec 2008 10:57:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: Nikos Katsikanis</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-15006</link>
		<author>Nikos Katsikanis</author>
		<pubDate>Thu, 28 Aug 2008 10:55:44 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-15006</guid>
		<description>This is super useful code dude :)</description>
		<content:encoded><![CDATA[<p>This is super useful code dude :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DhanaLakshmi</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-6173</link>
		<author>DhanaLakshmi</author>
		<pubDate>Thu, 17 Jan 2008 12:18:10 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-6173</guid>
		<description>Thanks for the post. It is useful as the data I get from a webservice is in the form of string as u used.</description>
		<content:encoded><![CDATA[<p>Thanks for the post. It is useful as the data I get from a webservice is in the form of string as u used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sm</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2737</link>
		<author>sm</author>
		<pubDate>Fri, 21 Sep 2007 15:12:19 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2737</guid>
		<description>Thanks. I definitely need a lot of information with Flex and m going to join this community right away!</description>
		<content:encoded><![CDATA[<p>Thanks. I definitely need a lot of information with Flex and m going to join this community right away!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2730</link>
		<author>peterd</author>
		<pubDate>Fri, 21 Sep 2007 14:06:28 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2730</guid>
		<description>Glad I could help. I also highly recommend subscribing to the FlexCoders mailing list, if you haven't already. (see http://flex.org/community/#mailinglists for links and details on various Flex-related mailing lists)

At over 7650 members, it is quite an active and informative list. Although one word of warning, it can get very busy (~3000  posts a month), so make sure you set up the appropriate filters otherwise your inbox can get a bit overwhelmed.

Happy Flexing!
Peter</description>
		<content:encoded><![CDATA[<p>Glad I could help. I also highly recommend subscribing to the FlexCoders mailing list, if you haven&#8217;t already. (see <a href="http://flex.org/community/#mailinglists" rel="nofollow">http://flex.org/community/#mailinglists</a> for links and details on various Flex-related mailing lists)</p>
<p>At over 7650 members, it is quite an active and informative list. Although one word of warning, it can get very busy (~3000  posts a month), so make sure you set up the appropriate filters otherwise your inbox can get a bit overwhelmed.</p>
<p>Happy Flexing!<br />
Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sm</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2727</link>
		<author>sm</author>
		<pubDate>Fri, 21 Sep 2007 13:37:15 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2727</guid>
		<description>Thanks a million Peter!!!u r a godsend!!that did work..i have been stuck on it fr 3 days and with such little examples on dynamic grids available online your help really meant a lot to me!!!</description>
		<content:encoded><![CDATA[<p>Thanks a million Peter!!!u r a godsend!!that did work..i have been stuck on it fr 3 days and with such little examples on dynamic grids available online your help really meant a lot to me!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2690</link>
		<author>peterd</author>
		<pubDate>Thu, 20 Sep 2007 22:06:51 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2690</guid>
		<description>sm,

Does the following work for you? Note that I have a slight "workaround" where I'm storing the currently selected header by listening for the data grid control's &lt;code&gt;headerRelease&lt;/code&gt; event.

&lt;pre class="code"&gt;
&#60;?xml version="1.0" encoding="utf-8"?&#62;
&#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&#62;
    
    &#60;mx:Script&#62;
        &#60;![CDATA[
            import mx.events.DataGridEvent;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.controls.DataGrid;
            import mx.utils.ObjectUtil;

            private var dataGrid:DataGrid;
            private var col:String;

            private function init():void {
                /* name column */
                var nameCol:DataGridColumn = new DataGridColumn("name");
                /* col1 column */
                var col1Col:DataGridColumn = new DataGridColumn("col1");
                col1Col.sortCompareFunction = numericSort;
                /* col2 column */
                var col2Col:DataGridColumn = new DataGridColumn("col2");
                col2Col.sortCompareFunction = numericSort;
                /* col3 column */
                var col3Col:DataGridColumn = new DataGridColumn("col3");
                col3Col.sortCompareFunction = numericSort;
                /* col4 column */
                var col4Col:DataGridColumn = new DataGridColumn("col4");
                col4Col.sortCompareFunction = numericSort;

                var cols:Array = [];
                cols.push(nameCol);
                cols.push(col1Col);
                cols.push(col2Col);
                cols.push(col3Col);
                cols.push(col4Col);

                dataGrid = new DataGrid();
                dataGrid.dataProvider = arr;
                dataGrid.columns = cols;
                dataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, dataGrid_headerRelease);
                addChild(dataGrid);
            }

            private function dataGrid_headerRelease(evt:DataGridEvent):void {
                col = dataGrid.columns[evt.columnIndex].dataField;
            }

            private function numericSort(itemA:Object, itemB:Object):int {
                return ObjectUtil.numericCompare(itemA[col], itemB[col]);
            }
        ]]&#62;
    &#60;/mx:Script&#62;

    &#60;mx:Array id="arr"&#62;
        &#60;mx:Object name="one" col1="1" col2="0.59" col3="1.5" col4="40" /&#62;
        &#60;mx:Object name="two" col1="2" col2="0.32" col3="1.1" col4="-12" /&#62;
        &#60;mx:Object name="three" col1="3" col2="0.82" col3="2.9" col4="22" /&#62;
        &#60;mx:Object name="four" col1="4" col2="0.29" col3="-0.2" col4="25" /&#62;
        &#60;mx:Object name="five" col1="5" col2="0.33" col3="5.0" col4="37" /&#62;
        &#60;mx:Object name="six" col1="6" col2="0.51" col3="-2.3" col4="-11" /&#62;
        &#60;mx:Object name="seven" col1="7" col2="0.70" col3="4.1" col4="-9" /&#62;
    &#60;/mx:Array&#62;

&#60;/mx:Application&#62;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>sm,</p>
<p>Does the following work for you? Note that I have a slight &#8220;workaround&#8221; where I&#8217;m storing the currently selected header by listening for the data grid control&#8217;s <code>headerRelease</code> event.</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.events.DataGridEvent;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.controls.DataGrid;
            import mx.utils.ObjectUtil;

            private var dataGrid:DataGrid;
            private var col:String;

            private function init():void {
                /* name column */
                var nameCol:DataGridColumn = new DataGridColumn("name");
                /* col1 column */
                var col1Col:DataGridColumn = new DataGridColumn("col1");
                col1Col.sortCompareFunction = numericSort;
                /* col2 column */
                var col2Col:DataGridColumn = new DataGridColumn("col2");
                col2Col.sortCompareFunction = numericSort;
                /* col3 column */
                var col3Col:DataGridColumn = new DataGridColumn("col3");
                col3Col.sortCompareFunction = numericSort;
                /* col4 column */
                var col4Col:DataGridColumn = new DataGridColumn("col4");
                col4Col.sortCompareFunction = numericSort;

                var cols:Array = [];
                cols.push(nameCol);
                cols.push(col1Col);
                cols.push(col2Col);
                cols.push(col3Col);
                cols.push(col4Col);

                dataGrid = new DataGrid();
                dataGrid.dataProvider = arr;
                dataGrid.columns = cols;
                dataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, dataGrid_headerRelease);
                addChild(dataGrid);
            }

            private function dataGrid_headerRelease(evt:DataGridEvent):void {
                col = dataGrid.columns[evt.columnIndex].dataField;
            }

            private function numericSort(itemA:Object, itemB:Object):int {
                return ObjectUtil.numericCompare(itemA[col], itemB[col]);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Array id="arr"&gt;
        &lt;mx:Object name="one" col1="1" col2="0.59" col3="1.5" col4="40" /&gt;
        &lt;mx:Object name="two" col1="2" col2="0.32" col3="1.1" col4="-12" /&gt;
        &lt;mx:Object name="three" col1="3" col2="0.82" col3="2.9" col4="22" /&gt;
        &lt;mx:Object name="four" col1="4" col2="0.29" col3="-0.2" col4="25" /&gt;
        &lt;mx:Object name="five" col1="5" col2="0.33" col3="5.0" col4="37" /&gt;
        &lt;mx:Object name="six" col1="6" col2="0.51" col3="-2.3" col4="-11" /&gt;
        &lt;mx:Object name="seven" col1="7" col2="0.70" col3="4.1" col4="-9" /&gt;
    &lt;/mx:Array&gt;

&lt;/mx:Application&gt;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: sm</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2689</link>
		<author>sm</author>
		<pubDate>Thu, 20 Sep 2007 21:45:25 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2689</guid>
		<description>Thanks Peter!Actually I have a datagrid that is getting generated dynamically using actionscript. The sortcomparefunction seems to work when i put it in the mxml tag but when i try using it in actionscript it fails!
eg if i do
&lt;pre class="code"&gt;
&lt;em&gt;datagridcolumnname&lt;/em&gt;.sortCompareFunction = numericSort;
&lt;/pre&gt;
in the mx:Script tags</description>
		<content:encoded><![CDATA[<p>Thanks Peter!Actually I have a datagrid that is getting generated dynamically using actionscript. The sortcomparefunction seems to work when i put it in the mxml tag but when i try using it in actionscript it fails!<br />
eg if i do</p>
<pre class="code">
<em>datagridcolumnname</em>.sortCompareFunction = numericSort;
</pre>
<p>in the mx:Script tags</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2681</link>
		<author>peterd</author>
		<pubDate>Thu, 20 Sep 2007 19:09:19 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2681</guid>
		<description>sm,

Not sure if there is a better way, but this was the first idea that came to mind. Surprisingly, it seems to work, based on my approximate 90 seconds of testing.

&lt;pre class="code"&gt;
&#60;?xml version="1.0" encoding="utf-8"?&#62;
&#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"&#62;

    &#60;mx:Script&#62;
        &#60;![CDATA[
            import mx.utils.ObjectUtil;

            private var col:String;

            private function numericSort(itemA:Object, itemB:Object):int {
                return ObjectUtil.numericCompare(itemA[col], itemB[col]);
            }
        ]]&#62;
    &#60;/mx:Script&#62;
    
    &#60;mx:Array id="arr"&#62;
        &#60;mx:Object name="one" col1="1" col2="0.59" col3="1.5" col4="40" /&#62;
        &#60;mx:Object name="two" col1="2" col2="0.32" col3="1.1" col4="-12" /&#62;
        &#60;mx:Object name="three" col1="3" col2="0.82" col3="2.9" col4="22" /&#62;
        &#60;mx:Object name="four" col1="4" col2="0.29" col3="-0.2" col4="25" /&#62;
        &#60;mx:Object name="five" col1="5" col2="0.33" col3="5.0" col4="37" /&#62;
        &#60;mx:Object name="six" col1="6" col2="0.51" col3="-2.3" col4="-11" /&#62;
        &#60;mx:Object name="seven" col1="7" col2="0.70" col3="4.1" col4="-9" /&#62;
    &#60;/mx:Array&#62;

    &#60;mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            headerRelease="col = dataGrid.columns[event.columnIndex].dataField;"&#62;
        &#60;mx:columns&#62;
            &#60;mx:DataGridColumn dataField="name" /&#62;
            &#60;mx:DataGridColumn dataField="col1"
                    sortCompareFunction="numericSort" /&#62;
            &#60;mx:DataGridColumn dataField="col2"
                    sortCompareFunction="numericSort" /&#62;
            &#60;mx:DataGridColumn dataField="col3"
                    sortCompareFunction="numericSort" /&#62;
            &#60;mx:DataGridColumn dataField="col4"
                    sortCompareFunction="numericSort" /&#62;
        &#60;/mx:columns&#62;
    &#60;/mx:DataGrid&#62;

&#60;/mx:Application&#62;
&lt;/pre&gt;

Hope that helps,
Peter</description>
		<content:encoded><![CDATA[<p>sm,</p>
<p>Not sure if there is a better way, but this was the first idea that came to mind. Surprisingly, it seems to work, based on my approximate 90 seconds of testing.</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.utils.ObjectUtil;

            private var col:String;

            private function numericSort(itemA:Object, itemB:Object):int {
                return ObjectUtil.numericCompare(itemA[col], itemB[col]);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Array id="arr"&gt;
        &lt;mx:Object name="one" col1="1" col2="0.59" col3="1.5" col4="40" /&gt;
        &lt;mx:Object name="two" col1="2" col2="0.32" col3="1.1" col4="-12" /&gt;
        &lt;mx:Object name="three" col1="3" col2="0.82" col3="2.9" col4="22" /&gt;
        &lt;mx:Object name="four" col1="4" col2="0.29" col3="-0.2" col4="25" /&gt;
        &lt;mx:Object name="five" col1="5" col2="0.33" col3="5.0" col4="37" /&gt;
        &lt;mx:Object name="six" col1="6" col2="0.51" col3="-2.3" col4="-11" /&gt;
        &lt;mx:Object name="seven" col1="7" col2="0.70" col3="4.1" col4="-9" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            headerRelease="col = dataGrid.columns[event.columnIndex].dataField;"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="name" /&gt;
            &lt;mx:DataGridColumn dataField="col1"
                    sortCompareFunction="numericSort" /&gt;
            &lt;mx:DataGridColumn dataField="col2"
                    sortCompareFunction="numericSort" /&gt;
            &lt;mx:DataGridColumn dataField="col3"
                    sortCompareFunction="numericSort" /&gt;
            &lt;mx:DataGridColumn dataField="col4"
                    sortCompareFunction="numericSort" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p>Hope that helps,<br />
Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sm</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2666</link>
		<author>sm</author>
		<pubDate>Thu, 20 Sep 2007 16:02:33 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-2666</guid>
		<description>how do u make this sort function generic i.e if you have many numeric columns in a datagrid, do you need to write a comparator function for each column?</description>
		<content:encoded><![CDATA[<p>how do u make this sort function generic i.e if you have many numeric columns in a datagrid, do you need to write a comparator function for each column?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charlie</title>
		<link>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-208</link>
		<author>Charlie</author>
		<pubDate>Thu, 16 Aug 2007 00:32:12 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/#comment-208</guid>
		<description>I like to keep my dates in the database as YYYY-MM-DD, this way they sort without help. Then all you need is a labelFunction to display them as you like them.</description>
		<content:encoded><![CDATA[<p>I like to keep my dates in the database as YYYY-MM-DD, this way they sort without help. Then all you need is a labelFunction to display them as you like them.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
