4 Replies Latest reply on Nov 6, 2008 11:09 AM by m_hartnett

    sortCompareFunction

    Hugo_Core
      Hi all,

      In my App I need a custom sortCompareFunction to resove my problem.
      My problem is, if I have a column with "1, 2, 1, 3, null, 2, null', by default flex sort like this "null, null, 1, 1, 2, 2, 3" or "3, 2, 2, 1, 1, null, null". It means, first cames the null then the values. I need "1, 1, 2, 2, 3, null, null"

      How can I have the reverse, first the values ASC, then the nulls.

      Thank you so much.
      Core Reagards
        • 1. Re: sortCompareFunction
          Hugo_Core Level 1
          If sortCompareFunction its not the best way to sort, first values then nulls, tell me...
          • 2. Re: sortCompareFunction
            Hugo_Core Level 1
            If sortCompareFunction its not the best way to sort, first values then nulls, tell me...
            • 3. Re: sortCompareFunction
              Hugo_Core Level 1
              If sortCompareFunction its not the best way to sort, first values then nulls, tell me...
              • 4. Re: sortCompareFunction
                m_hartnett Level 3
                You could do something like this. The sort method may have to be tweeked for your needs.

                <mx:Application
                xmlns:mx=" http://www.adobe.com/2006/mxml"
                layout="absolute"
                creationComplete="initApp()"
                viewSourceURL="srcview/index.html">
                <mx:Script>
                <![CDATA[
                import mx.utils.ObjectUtil;
                import mx.collections.XMLListCollection;
                import mx.binding.utils.BindingUtils;
                import mx.collections.ArrayCollection;
                import mx.core.Application;
                import flash.events.*;
                import mx.events.DataGridEvent;
                import mx.controls.TextInput;

                [Bindable]private var dgSource : XMLList = new XMLList;

                [Bindable]
                public var somedata:XML =
                <testData>
                <dataItem>
                <col1>1</col1>
                </dataItem>
                <dataItem>
                <col1>2</col1>
                </dataItem>
                <dataItem>
                <col1>3</col1>
                </dataItem>
                <dataItem>
                <col1></col1>
                </dataItem>
                <dataItem>
                <col1>3</col1>
                </dataItem>
                <dataItem>
                <col1>1</col1>
                </dataItem>
                <dataItem>
                <col1></col1>
                </dataItem>
                <dataItem>
                <col1>2</col1>
                </dataItem>
                </testData>;

                private function initApp() : void {
                dgSource = new XMLList(somedata).children();
                }

                private function sortNumeric(obj1:Object, obj2:Object):int {

                //These two statements are the same as an if / else statement
                //set the value1/2 to 99999 or something that always sorts to the end
                var value1:Number = (obj1.col1 == "" || obj1.col1 == null) ? 99999 : new Number(obj1.col1);
                var value2:Number = (obj2.col1 == "" || obj2.col1 == null) ? 99999 : new Number(obj2.col1);

                if (value1 < value2) {
                return -1;
                }
                else if (value1 > value2) {
                return 1;
                }
                else {
                return 0;
                }
                }







                ]]>
                </mx:Script>

                <mx:DataGrid x="10" y="10" width="1098" height="322" borderColor="#71B7E7"
                alternatingItemColors="[#D6E0E6, #FFFFFF]"
                borderStyle="inset"
                id="profileViewer"
                dataProvider="{dgSource}"
                horizontalScrollPolicy="auto">
                <mx:columns>
                <mx:DataGridColumn headerText="col1" dataField="col1" sortCompareFunction="sortNumeric"/>
                <mx:DataGridColumn headerText="col1 NoCustomSort" dataField="col1"/>

                </mx:columns>
                </mx:DataGrid>


                </mx:Application>