iimanii - Support for custom sort plugins is being removed? Or is it just a bug

iimanii - Support for custom sort plugins is being removed? Or is it just a bug

allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

So basically I have these non-linear sorting functions which tends to push down the "null" values either in asc or desc, I'm also doing some formatting before sorting ( "pre" sort function ).

As of ver 1.10, the custom sorting functions will not get called if there is a "pre" function defined. (check line 4400)

I've also found the comment "4427: // Depreciated - remove in 1.11 (providing a plug-in option)"

So whats that all about ?

Note from Allan - Apologies, I deleted your discussion by mistake as I had it open in two tabs and thought it was a duplicate... doh.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Its an optimisation. If there is a pre function, then the external sort functions don't need to be called since the pre function should be formatting the data into a sortable format using the Javascript Array.sort() method.

    If you need sac and desc functions, don't provide a pre function.

    I'm actually not sure that I will remove the asc and desc support in 1.11, but it isn't needed in 99.9% of cases which is why I've marked it for removal at the moment...

    Allan

  • iimaniiiimanii Posts: 3Questions: 0Answers: 0

    well basically, it seems that the pre values are cached, which would make the running time faster than just embedding the code inside asc and desc and redoing the work every time

    True the asc and desc would be the same in most of the cases, but in the case I've mentioned above I have to define those functions.

    In the mean time I had to alter the datatable source code to be able to use the functionality.

    anyways I hope you keep it. I also hope you can consider the following as a replacement for code starting @ line 4400 within _fnSort

     var fn = [];
     var asc  = function(x, y) {return x < y ? -1 : x > y ? 1 : 0;};
     var desc = function(x, y) {return x < y ? 1 : x > y ? -1 : 0;};
     for( k = 0; k < aSort.length; k++)
            fn[k] = oExtSort[ aSort[k].type+"-"+aSort[k].dir ] ||
                       (aSort[k].formatter ? (aSort[k].dir === 'asc' ? asc : desc) : oExtSort[ "string-"+aSort[k].dir ]);
    
     displayMaster.sort( function ( a, b ) {
                var  x, y, k, test, sort,
                       len=aSort.length,
                       dataA = aoData[a]._aSortData,
                       dataB = aoData[b]._aSortData;
    
                       for ( k=0 ; k<len ; k++ ) {
                               sort = aSort[k];
    
                               x = dataA[ sort.col ];
                               y = dataB[ sort.col ];
    
                               test = fn[k](x , y);
                               if ( test !== 0 ) 
                                     return test;
                               
                       }
    
                       x = aiOrig[a];
                       y = aiOrig[b];
                       return x<y ? -1 : x>y ? 1 : 0;
     } );
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Rather then using pre and modifying the source, wouldn't you just do something like the natural sorting plug-in does and simply call a formatting function: http://datatables.net/plug-ins/sorting/natural . You trade memory for processor time, but it will work without modifying the source.

    Allan

  • iimaniiiimanii Posts: 3Questions: 0Answers: 0
    edited May 2014

    It seems that using "mRender" would solve the issue in a more efficient way, If I pre-compute the sort values

                function mDataCustom(data, type, row) {
                    if(type === 'sort') {
                       return data._sort
                    }
                    return data;
                }
    
This discussion has been closed.