Multi Column sort and Custom Type fails to sort second column

Multi Column sort and Custom Type fails to sort second column

JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

I've come across an issue where datatables is failing to sort second column in a order when using a custom type

see: http://live.datatables.net/dirajulu/1/edit

When you press the "button" it shows the icon to indicate both columns are being sorted but the console output is not showing for the second columns sorting.

Repeated pressing it appears to be causing a asc/desc toggle to occur on the 2nd column?

This question has an accepted answers - jump to answer

Answers

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    To clarify, the attached is only a very basic example, my actual requirement is around dates/nulls last, just wanted to confirm it wasn't something else in my code causing the issue, but can confirm from that example it appears to ignore the second order even on a very basic type ordering.

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    Chopped my example down to the bare minimum:
    http://live.datatables.net/qipifuza/1/edit

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    Could still do with an answer to this, I assume it is a bug, but is it one that'll ever be fixed?

  • awelchawelch Posts: 38Questions: 1Answers: 3

    I ran into a similar (or possibly the same) issue. I can't tell definitively but from your statement that your actual requirements are around dates/nulls last I am assuming you are using the Absolute sorting plugin (or a custom version you created). First, the issue with your examples is that you aren't accounting for the equals case. The return statements in your order functions should be as follows:

    ...
    return (a>b)?1:(a<b)?-1:0;
    ...
    return (a<b)?1:(a<b)?-1:0;
    

    The same issue is happening in the Absolute sorting plugin. The ordering methods are missing the equals case when checking the alwaysTop and alwaysBottom arrays. The code needs to be updated to the following:

    // Ascending ordering method
        o.asc = function ( a, b, isNumber ) {
            if ( o.alwaysTop[ a ] && o.alwaysTop[ b ] ) {
                return 0;
            }  
            else if ( o.alwaysBottom[ a ] && o.alwaysBottom[ b ] ) {
                return 0;
            }      
            else if ( o.alwaysTop[ a ] || o.alwaysBottom[ b ] ) {
                return -1;
            }
            else if ( o.alwaysBottom[ a ] || o.alwaysTop[ b ] ) {
                return 1;
            }
     
            if ( isNumber ) {
                // Cast as a number if required
                if ( typeof a === 'string' ) {
                    a = a.replace(/[^\d\-\.]/g, '') * 1;
                }
                if ( typeof b === 'string' ) {
                    b = b.replace(/[^\d\-\.]/g, '') * 1;
                }
            }
     
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        };
     
        // Descending ordering method
        o.desc = function ( a, b, isNumber ) {
            if ( o.alwaysTop[ a ] && o.alwaysTop[ b ] ) {
                return 0;
            }  
            else if ( o.alwaysBottom[ a ] && o.alwaysBottom[ b ] ) {
                return 0;
            }
            else if ( o.alwaysTop[ a ] || o.alwaysBottom[ b ] ) {
                return -1;
            }
            else if ( o.alwaysBottom[ a ] || o.alwaysTop[ b ] ) {
                return 1;
            }
     
            if ( isNumber ) {
                if ( typeof a === 'string' ) {
                    a = a.replace(/[^\d\-\.]/g, '') * 1;
                }
                if ( typeof b === 'string' ) {
                    b = b.replace(/[^\d\-\.]/g, '') * 1;
                }
            }
     
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        };
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yep, I'd agree something looks wonky there. It's calling the sort for the first column, but then there's nothing for the second. I'll take a deeper nose and report back.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi again all,

    Nope, there's not a problem there, tis all good. The issue, as shown in this even more simplified example, is that the comparison functions were missing the 'equals' check:

    $.fn.dataTable.ext.type.order["special-desc"] = function(a,b) {
      if ( a==b ) {
        return 0;
      }
      return (a<b)?1:-1;
    };
    

    Cheers,

    Colin

This discussion has been closed.