5 Replies Latest reply on Aug 17, 2006 11:13 AM by peterent


    trycold Level 1

      Is there a way to sort a date field in a data grid that is using a labelfunction to get the date value. How to use sortCompareFunction with that?

        • 1. Re: sortCompareFunction
          peterent Level 2
          Use the Collection's sort object. It sorts the data in the Collection, not what is displayed.
          • 2. Re: sortCompareFunction
            trycold Level 1

            Can you please give an example. I have something like below. So how do I use the sort:

            <mx:DataGrid dataProvider="{Test}" width="100%" id="testDataGrid" height="100%" >
            <mx:DataGridColumn dataField="DateStarted" color="#666666" headerText="Started" labelFunction="SubmittedFucn" width="85" />
            • 3. Re: sortCompareFunction
              peterent Level 2

              import mx.collections.Sort;

              var s:Sort = new Sort();
              sort.fields = [ new SortField("DateStarted") ];
              sort.compareFunction = function(a:Object, b:Object, fields:Array = null ) {
              if( a.DateStarted.time < b.DateStarted.time ) return -1;
              else if( a.DateStarted.time > b.DateStarted.time ) return 1;
              else return 0;
              Test.sort = s;
              Test.refresh(); // important to do this

              You need to have your own compareFunction because Date is not a String nor a Number, although the time property is a Number and can be compared.

              An alternative is to start the time of the Date (milliseconds since Jan 1970) and use the labelFunction to convert it back into a Date and format it. If you do this then you do not need the sort's compareFunction.
              • 4. Re: sortCompareFunction
                trycold Level 1

                Thanks for the exampl. I tried using it. It works only once to put in the order if I click again then it does not work. Another thing is if I have some rows which don't have any data in them then it gives an error. How do i solve this.

                • 5. Re: sortCompareFunction
                  peterent Level 2
                  If you want to resort, then do Test.refresh(); if there are nulls in your data you need to test for that in the sortFunction and determine how null values sort in your data (nulls before dates or nulls after).