2 Replies Latest reply on Aug 26, 2009 11:56 AM by msakrejda

    Issue with Number sort in flex datagrid

    Amit B Sutar

      Hi All,

      I have implemented Number sorting as mentioned in the below(code).

      I have placed the function that i am setting for the column that is to be sorted.

      This column contains formatted amount (e.g. $2,000.00), so flex considers it as String and sorts the data as String.

      In the function pasted below i am removing the formatting and then converting the unformatted string into a Number.

      This function works fine when the amount of data is not large. i.e. < 1000 records in datagrid.

      But when the records are > 1000, the sorting takes long time and most of the time the script takes more than 30 secs to run.

      The reason here is that when the amount of data is large then the comparisons increase and the method which i am calling in this method gets called twice as many times as the numericSort Function.

      This causes the delay and the error is thrown.


      Please help as i need a solution to this urgently.



      Here is the method

      Code :::


      public static function numericSort(obj1 : Object, obj2 : Object):int
      var d1 : Number;
      var d2 : Number;
      var index : int = Util.getColumnIndex("MARGIN");
      = Number(removeStringFormatting(obj1[index]));
      = Number(removeStringFormatting(obj2[index]));
      if (d1 < d2) {
      return -1;
      } else if (d1 > d2) {
      return 1;
      } else {
      return 0;
      catch(error : Error)
      Alert.show("Error in numericSort "+error.getStackTrace());
        • 1. Re: Issue with Number sort in flex datagrid
          Barna Biro Level 3

          Off topic: Having 1000 results displayed at once in a DataGrid ( heck, even 100-200 at once ) is dumb. Instead, you should break your data down to smaller pieces and use paging or something to traverse the whole collection of data. Displaying an imense bunch at once is just plain dumb.


          Regarding your problem: Since the data displayed in the DataGrid is formatted, you won't be having an easy time trying to sort that formated data. Instead, you should convert the data back to Number or integer ( or whatever might suit your needs ) and sort it that way. A custom compare function is good because you can perform the conversion back to the original state there and then sort the values ( or you can store a temporal list of the data that is unformatted, sort it, format it and display it - this is just one possible solution of the quite many ).

          • 2. Re: Issue with Number sort in flex datagrid
            msakrejda Level 4

            If you have control over the data, you could even send it over as numbers, but then provide a custom DataGridColumn labelFunction to display it as it currently looks. The sort will happen on the underlying numbers, so it will be much faster, but the appearance will be like what you currently have.


            Maciek Sakrejda
            Truviso, Inc.
            Truviso is hiring: http://www.truviso.com/company-careers.php?i=87