<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Creating a custom sort on a DataGrid control in Flex</title>
	<atom:link href="http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Sat, 11 Feb 2012 11:51:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tahir Azeem Alvi</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-9172</link>
		<dc:creator>Tahir Azeem Alvi</dc:creator>
		<pubDate>Thu, 28 Apr 2011 16:14:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-9172</guid>
		<description>Hi peter,

What is the event that gets fired when you click on a datagrid column to sort it?</description>
		<content:encoded><![CDATA[<p>Hi peter,</p>
<p>What is the event that gets fired when you click on a datagrid column to sort it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-6101</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Fri, 23 Oct 2009 01:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-6101</guid>
		<description>Thanks Peter,  this is awesome!!</description>
		<content:encoded><![CDATA[<p>Thanks Peter,  this is awesome!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sunny</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-3077</link>
		<dc:creator>Sunny</dc:creator>
		<pubDate>Mon, 01 Dec 2008 10:52:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-3077</guid>
		<description>Thany you for your code.
First I click the DataGrid&#039;s header, there&#039;s a small black triangle to show where it is working on.
Then I click the buttons upside, to resort the DataGrid. Can the small black triangle change the meantime?</description>
		<content:encoded><![CDATA[<p>Thany you for your code.<br />
First I click the DataGrid&#8217;s header, there&#8217;s a small black triangle to show where it is working on.<br />
Then I click the buttons upside, to resort the DataGrid. Can the small black triangle change the meantime?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-3075</link>
		<dc:creator>peterd</dc:creator>
		<pubDate>Fri, 03 Oct 2008 16:13:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-3075</guid>
		<description>Something like this:
&lt;pre class=&quot;code&quot;&gt;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.events.ListEvent;
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.utils.ObjectUtil;

            private function comboBox_change(evt:ListEvent):void {
                var item:Object = comboBox.selectedItem;

                var sortField:SortField = new SortField();
                sortField.descending = item.descending;
                sortField.compareFunction = item.compareFunc;

                var sort:Sort = new Sort();
                sort.fields = [sortField];

                xmlListColl.sort = sort;
                xmlListColl.refresh();
            }

            private function resetSort():void {
                xmlListColl.sort = null;
                xmlListColl.refresh();
            }

            private function dataGridCol_labelFunc(item:XML,
                        col:DataGridColumn):String {
                return item.*.(@name == col.dataField).text();
            }

            private function test2_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test2.text();
                var valueB:String = itemB.test2.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }

            private function test3_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test3.text();
                var valueB:String = itemB.test3.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:XML id=&quot;tests&quot; source=&quot;tests.xml&quot; /&gt;
    &lt;mx:XMLListCollection id=&quot;xmlListColl&quot; source=&quot;{tests.test}&quot; /&gt;

    &lt;mx:Array id=&quot;arr&quot;&gt;
        &lt;mx:Object label=&quot;Sort &#039;old&#039; ascending&quot;
                sortField=&quot;old&quot;
                descending=&quot;false&quot;
                compareFunc=&quot;{test2_compareFunc}&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;old&#039; descending&quot;
                sortField=&quot;old&quot;
                descending=&quot;true&quot;
                compareFunc=&quot;{test2_compareFunc}&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;new&#039; ascending&quot;
                sortField=&quot;new&quot;
                descending=&quot;false&quot;
                compareFunc=&quot;{test3_compareFunc}&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;new&#039; descending&quot;
                sortField=&quot;new&quot;
                descending=&quot;true&quot;
                compareFunc=&quot;{test3_compareFunc}&quot; /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:ApplicationControlBar dock=&quot;true&quot;&gt;
        &lt;mx:ComboBox id=&quot;comboBox&quot;
                dataProvider=&quot;{arr}&quot;
                prompt=&quot;Please select a sort...&quot;
                change=&quot;comboBox_change(event);&quot; /&gt;

        &lt;mx:Button label=&quot;Reset sort&quot;
                click=&quot;resetSort();&quot; /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id=&quot;dataGrid&quot;
            dataProvider=&quot;{xmlListColl}&quot;
            width=&quot;300&quot;
            verticalScrollPolicy=&quot;on&quot;
            sortableColumns=&quot;true&quot;&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField=&quot;old&quot;
                    labelFunction=&quot;dataGridCol_labelFunc&quot;
                    sortCompareFunction=&quot;test2_compareFunc&quot; /&gt;
            &lt;mx:DataGridColumn dataField=&quot;new&quot;
                    labelFunction=&quot;dataGridCol_labelFunc&quot;
                    sortCompareFunction=&quot;test3_compareFunc&quot;  /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
&lt;/pre&gt;

Peter</description>
		<content:encoded><![CDATA[<p>Something like this:</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"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.events.ListEvent;
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.utils.ObjectUtil;

            private function comboBox_change(evt:ListEvent):void {
                var item:Object = comboBox.selectedItem;

                var sortField:SortField = new SortField();
                sortField.descending = item.descending;
                sortField.compareFunction = item.compareFunc;

                var sort:Sort = new Sort();
                sort.fields = [sortField];

                xmlListColl.sort = sort;
                xmlListColl.refresh();
            }

            private function resetSort():void {
                xmlListColl.sort = null;
                xmlListColl.refresh();
            }

            private function dataGridCol_labelFunc(item:XML,
                        col:DataGridColumn):String {
                return item.*.(@name == col.dataField).text();
            }

            private function test2_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test2.text();
                var valueB:String = itemB.test2.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }

            private function test3_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test3.text();
                var valueB:String = itemB.test3.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:XML id="tests" source="tests.xml" /&gt;
    &lt;mx:XMLListCollection id="xmlListColl" source="{tests.test}" /&gt;

    &lt;mx:Array id="arr"&gt;
        &lt;mx:Object label="Sort 'old' ascending"
                sortField="old"
                descending="false"
                compareFunc="{test2_compareFunc}" /&gt;
        &lt;mx:Object label="Sort 'old' descending"
                sortField="old"
                descending="true"
                compareFunc="{test2_compareFunc}" /&gt;
        &lt;mx:Object label="Sort 'new' ascending"
                sortField="new"
                descending="false"
                compareFunc="{test3_compareFunc}" /&gt;
        &lt;mx:Object label="Sort 'new' descending"
                sortField="new"
                descending="true"
                compareFunc="{test3_compareFunc}" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:ComboBox id="comboBox"
                dataProvider="{arr}"
                prompt="Please select a sort..."
                change="comboBox_change(event);" /&gt;

        &lt;mx:Button label="Reset sort"
                click="resetSort();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            width="300"
            verticalScrollPolicy="on"
            sortableColumns="true"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="old"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test2_compareFunc" /&gt;
            &lt;mx:DataGridColumn dataField="new"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test3_compareFunc"  /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p>Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterd</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-3074</link>
		<dc:creator>peterd</dc:creator>
		<pubDate>Fri, 03 Oct 2008 16:06:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-3074</guid>
		<description>Dave,

A bit crude, but this should work:
&lt;pre class=&quot;code&quot;&gt;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
        layout=&quot;vertical&quot;
        verticalAlign=&quot;middle&quot;
        backgroundColor=&quot;white&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.events.ListEvent;
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.utils.ObjectUtil;

            private function comboBox_change(evt:ListEvent):void {
                var field:String = comboBox.selectedItem.sortField;
                var desc:Boolean = (comboBox.selectedItem.descending);

                var sort:Sort = new Sort();
                var sortField:SortField = new SortField();
                sortField.descending = desc;
                switch (field) {
                    case &quot;old&quot;:
                        sortField.compareFunction = test2_compareFunc;
                        break;
                    case &quot;new&quot;:
                        sortField.compareFunction = test3_compareFunc;
                        break;
                }
                sort.fields = [sortField];
                xmlListColl.sort = sort;
                xmlListColl.refresh();
            }

            private function resetSort():void {
                xmlListColl.sort = null;
                xmlListColl.refresh();
            }

            private function dataGridCol_labelFunc(item:XML, col:DataGridColumn):String {
                return item.*.(@name == col.dataField).text();
            }

            private function test2_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test2.text();
                var valueB:String = itemB.test2.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }

            private function test3_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test3.text();
                var valueB:String = itemB.test3.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:XML id=&quot;tests&quot; source=&quot;tests.xml&quot; /&gt;
    &lt;mx:XMLListCollection id=&quot;xmlListColl&quot; source=&quot;{tests.test}&quot; /&gt;

    &lt;mx:Array id=&quot;arr&quot;&gt;
        &lt;mx:Object label=&quot;Sort &#039;old&#039; ascending&quot;
                sortField=&quot;old&quot;
                descending=&quot;false&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;old&#039; descending&quot;
                sortField=&quot;old&quot;
                descending=&quot;true&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;new&#039; ascending&quot;
                sortField=&quot;new&quot;
                descending=&quot;false&quot; /&gt;
        &lt;mx:Object label=&quot;Sort &#039;new&#039; descending&quot;
                sortField=&quot;new&quot;
                descending=&quot;true&quot; /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:ApplicationControlBar dock=&quot;true&quot;&gt;
        &lt;mx:ComboBox id=&quot;comboBox&quot;
                dataProvider=&quot;{arr}&quot;
                change=&quot;comboBox_change(event);&quot; /&gt;

        &lt;mx:Button label=&quot;Reset sort&quot;
                click=&quot;resetSort();&quot; /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id=&quot;dataGrid&quot;
            dataProvider=&quot;{xmlListColl}&quot;
            width=&quot;300&quot;
            verticalScrollPolicy=&quot;on&quot;
            sortableColumns=&quot;true&quot;&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField=&quot;old&quot;
                    labelFunction=&quot;dataGridCol_labelFunc&quot;
                    sortCompareFunction=&quot;test2_compareFunc&quot; /&gt;
            &lt;mx:DataGridColumn dataField=&quot;new&quot;
                    labelFunction=&quot;dataGridCol_labelFunc&quot;
                    sortCompareFunction=&quot;test3_compareFunc&quot;  /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
&lt;/pre&gt;

Looking back on the code, you may be able to move the &lt;code&gt;sortField.compareFunction&lt;/code&gt; logic into the ComboBox data provider array instead of using a &lt;code&gt;switch&lt;/code&gt; statement in the &lt;code&gt;comboBox_change()&lt;/code&gt; function. I didn&#039;t think of that the first time. You may also want to set the &lt;code&gt;prompt&lt;/code&gt; property on the ComboBox.

Peter</description>
		<content:encoded><![CDATA[<p>Dave,</p>
<p>A bit crude, but this should work:</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"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.events.ListEvent;
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.utils.ObjectUtil;

            private function comboBox_change(evt:ListEvent):void {
                var field:String = comboBox.selectedItem.sortField;
                var desc:Boolean = (comboBox.selectedItem.descending);

                var sort:Sort = new Sort();
                var sortField:SortField = new SortField();
                sortField.descending = desc;
                switch (field) {
                    case "old":
                        sortField.compareFunction = test2_compareFunc;
                        break;
                    case "new":
                        sortField.compareFunction = test3_compareFunc;
                        break;
                }
                sort.fields = [sortField];
                xmlListColl.sort = sort;
                xmlListColl.refresh();
            }

            private function resetSort():void {
                xmlListColl.sort = null;
                xmlListColl.refresh();
            }

            private function dataGridCol_labelFunc(item:XML, col:DataGridColumn):String {
                return item.*.(@name == col.dataField).text();
            }

            private function test2_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test2.text();
                var valueB:String = itemB.test2.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }

            private function test3_compareFunc(itemA:XML, itemB:XML):int {
                var valueA:String = itemA.test3.text();
                var valueB:String = itemB.test3.text();
                return ObjectUtil.stringCompare(valueA, valueB);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:XML id="tests" source="tests.xml" /&gt;
    &lt;mx:XMLListCollection id="xmlListColl" source="{tests.test}" /&gt;

    &lt;mx:Array id="arr"&gt;
        &lt;mx:Object label="Sort 'old' ascending"
                sortField="old"
                descending="false" /&gt;
        &lt;mx:Object label="Sort 'old' descending"
                sortField="old"
                descending="true" /&gt;
        &lt;mx:Object label="Sort 'new' ascending"
                sortField="new"
                descending="false" /&gt;
        &lt;mx:Object label="Sort 'new' descending"
                sortField="new"
                descending="true" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:ComboBox id="comboBox"
                dataProvider="{arr}"
                change="comboBox_change(event);" /&gt;

        &lt;mx:Button label="Reset sort"
                click="resetSort();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            width="300"
            verticalScrollPolicy="on"
            sortableColumns="true"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="old"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test2_compareFunc" /&gt;
            &lt;mx:DataGridColumn dataField="new"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test3_compareFunc"  /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p>Looking back on the code, you may be able to move the <code>sortField.compareFunction</code> logic into the ComboBox data provider array instead of using a <code>switch</code> statement in the <code>comboBox_change()</code> function. I didn&#8217;t think of that the first time. You may also want to set the <code>prompt</code> property on the ComboBox.</p>
<p>Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/comment-page-1/#comment-3076</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Wed, 24 Sep 2008 09:58:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-3076</guid>
		<description>Thanks! this is very useful info!!
How can sorting a datagrid be accomplished using a ComboBox?

Dave</description>
		<content:encoded><![CDATA[<p>Thanks! this is very useful info!!<br />
How can sorting a datagrid be accomplished using a ComboBox?</p>
<p>Dave</p>
]]></content:encoded>
	</item>
</channel>
</rss>

