<?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: Creating a custom sort on a DataGrid control in Flex</title>
	<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/</link>
	<description>A bunch of examples for Adobe Flex and ActionScript</description>
	<pubDate>Fri, 05 Dec 2008 00:05:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: Sunny</title>
		<link>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-17071</link>
		<author>Sunny</author>
		<pubDate>Mon, 01 Dec 2008 10:52:14 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-17071</guid>
		<description>Thany you for your code. 
First I click the DataGrid's header, there'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-15991</link>
		<author>peterd</author>
		<pubDate>Fri, 03 Oct 2008 16:13:54 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-15991</guid>
		<description>Something like this:
&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"&#62;

    &#60;mx:Script&#62;
        &#60;![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);
            }
        ]]&#62;
    &#60;/mx:Script&#62;

    &#60;mx:XML id="tests" source="tests.xml" /&#62;
    &#60;mx:XMLListCollection id="xmlListColl" source="{tests.test}" /&#62;

    &#60;mx:Array id="arr"&#62;
        &#60;mx:Object label="Sort 'old' ascending"
                sortField="old"
                descending="false"
                compareFunc="{test2_compareFunc}" /&#62;
        &#60;mx:Object label="Sort 'old' descending"
                sortField="old"
                descending="true"
                compareFunc="{test2_compareFunc}" /&#62;
        &#60;mx:Object label="Sort 'new' ascending"
                sortField="new"
                descending="false"
                compareFunc="{test3_compareFunc}" /&#62;
        &#60;mx:Object label="Sort 'new' descending"
                sortField="new"
                descending="true"
                compareFunc="{test3_compareFunc}" /&#62;
    &#60;/mx:Array&#62;

    &#60;mx:ApplicationControlBar dock="true"&#62;
        &#60;mx:ComboBox id="comboBox"
                dataProvider="{arr}"
                prompt="Please select a sort..."
                change="comboBox_change(event);" /&#62;

        &#60;mx:Button label="Reset sort"
                click="resetSort();" /&#62;
    &#60;/mx:ApplicationControlBar&#62;

    &#60;mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            width="300"
            verticalScrollPolicy="on"
            sortableColumns="true"&#62;
        &#60;mx:columns&#62;
            &#60;mx:DataGridColumn dataField="old"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test2_compareFunc" /&#62;
            &#60;mx:DataGridColumn dataField="new"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test3_compareFunc"  /&#62;
        &#60;/mx:columns&#62;
    &#60;/mx:DataGrid&#62;

&#60;/mx:Application&#62;
&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-15990</link>
		<author>peterd</author>
		<pubDate>Fri, 03 Oct 2008 16:06:02 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-15990</guid>
		<description>Dave,

A bit crude, but this should work:
&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"&#62;

    &#60;mx:Script&#62;
        &#60;![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);
            }
        ]]&#62;
    &#60;/mx:Script&#62;

    &#60;mx:XML id="tests" source="tests.xml" /&#62;
    &#60;mx:XMLListCollection id="xmlListColl" source="{tests.test}" /&#62;

    &#60;mx:Array id="arr"&#62;
        &#60;mx:Object label="Sort 'old' ascending"
                sortField="old"
                descending="false" /&#62;
        &#60;mx:Object label="Sort 'old' descending"
                sortField="old"
                descending="true" /&#62;
        &#60;mx:Object label="Sort 'new' ascending"
                sortField="new"
                descending="false" /&#62;
        &#60;mx:Object label="Sort 'new' descending"
                sortField="new"
                descending="true" /&#62;
    &#60;/mx:Array&#62;

    &#60;mx:ApplicationControlBar dock="true"&#62;
        &#60;mx:ComboBox id="comboBox"
                dataProvider="{arr}"
                change="comboBox_change(event);" /&#62;

        &#60;mx:Button label="Reset sort"
                click="resetSort();" /&#62;
    &#60;/mx:ApplicationControlBar&#62;

    &#60;mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            width="300"
            verticalScrollPolicy="on"
            sortableColumns="true"&#62;
        &#60;mx:columns&#62;
            &#60;mx:DataGridColumn dataField="old"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test2_compareFunc" /&#62;
            &#60;mx:DataGridColumn dataField="new"
                    labelFunction="dataGridCol_labelFunc"
                    sortCompareFunction="test3_compareFunc"  /&#62;
        &#60;/mx:columns&#62;
    &#60;/mx:DataGrid&#62;

&#60;/mx:Application&#62;
&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'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-15818</link>
		<author>Dave</author>
		<pubDate>Wed, 24 Sep 2008 09:58:57 +0000</pubDate>
		<guid>http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#comment-15818</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>
