Datetime-columnsorting with momentJS - different behaviour asc/desc needed

Datetime-columnsorting with momentJS - different behaviour asc/desc needed

bytepoolbytepool Posts: 2Questions: 1Answers: 0

Hello together, I am using the "Ultimate Date / Time sorting" (http://datatables.net/plug-ins/sorting/datetime-moment) to sort a datetime-column with momentJS.

It's working pretty fine, but I need to differentiate between asc/desc-ordering, but I can't do

    types.order[ 'moment-'+format+'-asc' ] = function ( d ) {
        //do something
    };

    types.order[ 'moment-'+format+'-desc' ] = function ( d ) {
        //do something
    };

instead of

    types.order[ 'moment-'+format+'-pre' ] = function ( d ) {...

Is there a way to achieve that behaviour?

(What I try to do is that cells with a '-' (dash) are sorted to the bottom of the column not matter which direction)

Answers

  • allanallan Posts: 64,020Questions: 1Answers: 10,555 Site admin

    but I can't do [...]

    Why not? It should be quite possible to remove the -pre method and then replace it with -asc and -desc that will perform the required actions for each option. Could you link to a page showing the problem perhaps?

    Out of interest, what is the different action? I've often considered removing that ability in the core and leaving only the -pre option as it adds a few extra KB to the library size!

    Allan

  • bytepoolbytepool Posts: 2Questions: 1Answers: 0

    when I remove the -pre and replace it with -asc, -desc it jumps in, but the console.log (to check if the function got called) gets logged 7498 times!

    types.order[ 'moment-'+format+'-asc' ] = function ( d ) {
        console.log('asc');
        return d === '-' || d === null ? Infinity : parseInt( moment( d.replace ? d.replace(/<.*?>/g, '') : d, format, locale, true ).format( 'x' ), 10 );
    };
    
    types.order[ 'moment-'+format+'-desc' ] = function ( d ) {
        console.log('desc');
        return d === '-' || d === null ? -Infinity : parseInt( moment( d.replace ? d.replace(/<.*?>/g, '') : d, format, locale, true ).format( 'x' ), 10 );
    };
    

    I have a column with dates and dashes '-' and the I want to sort the dashes always to the end of the list, so I return

    Date (unsorted)                  Date (asc)                   Date (desc)
    28.11.2014, 16:17              27.11.2014, 13:54          27.02.2015, 16:09
             -                     28.11.2014, 16:17          28.11.2014, 16:17
    27.11.2014, 13:54              27.02.2015, 16:09          27.11.2014, 13:54
            -                           -                                 -
    27.02.2015, 16:09                   -                                 -
    
  • allanallan Posts: 64,020Questions: 1Answers: 10,555 Site admin

    but the console.log (to check if the function got called) gets logged 7498 times!

    So it does actually work, but you are concerned about the performance? This is exactly what the -pre option was designed for. It is basically a preformatter, so any calculations to format the date for sorting should be done in -pre and then -asc and -desc which will be called a lot (as is the nature of sorting algorithms - which one exactly is used is defined by the browser being used, not DataTables).

    So what you could do is do the Moment parsing in -pre and then a trivial check to see if the value is an integer or not in the two sorting functions.

    See also the manual for details.

    Allan

This discussion has been closed.