1 Reply Latest reply on Dec 7, 2009 8:03 PM by PhilipKeiter

    Custom sorting in data grid

      Sorting entire records

      Assume you have an array of 20 items (master array), and first 5 items are pulled into another array (paged array),
      Which is the dataprovider for the datagrid. And the datagrid has 10 columns.
      When the user sorts on the datagrid, sorting should happen on the 20 records, and
      Not on the 5 records.
      What i tried:

      Clicking on a datagrid column header will sort only the data provider of the datagrid. i.e., the
      5 records. In order to sort all 20 records, sorting should take place on the master array,
      and not on the paged array. Sorting on 20 items seems to be impossible.
      The only logic we could think of was that, on the headerRelease of datagrid, capture the
      column header clicked(not sure if you can capture it or not). And write custom function
      for sorting based on that column. Since there are 10 columns, the number of functions
      or number of blocks of code to be written will Be 10. So did not feel to be feasible and optimal solution.

      Please advice.
        • 1. Re: Custom sorting in data grid
          PhilipKeiter Level 2

          Hello chhavs,


          If you listen to the DataGrid's headerRelease event, the event has a property columnIndex to let you know which column the user wanted to sort. So you don't need 10 functions, just one with a switch statement, which is reasonable.


          private function handleHeaderRelease(event:DataGridEvent):void


          var sort:Sort = new Sort();

          var field:SortField;

          switch (event.columnIndex)


          case 0:

          field = new SortField("fieldA");


          case 1:

          field = new SortField("fieldB");


          case ...


          sort.fields = [field];

          masterRecords.sort = sort;