improvement to num-html

improvement to num-html

bwlangbwlang Posts: 20Questions: 0Answers: 0
edited March 2011 in Plug-ins
Here's a version of num-html that can deal with blanks.

I've just added a bit of isNaN to the return statements.

[code]
//sets up numeric sorting of links
jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
};
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sounds like a fair improvement - since its possible to have DataTables try and do numeric sorting on non-numeric data. However, might it be an idea to do the isNaN check before doing the numeric comparison (x
  • bwlangbwlang Posts: 20Questions: 0Answers: 0
    I'm not sure which is faster... this does work for me.
    (see http://polbase.neb.com/polymerases )
    I did explore using Infinity if one or the other was NaN, but that was cumbersome.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I was meaning something like this:

    [code]
    //sets up numeric sorting of links
    jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    if ( isNaN( x ) ) { x = -1; };
    y = parseFloat( y );
    if ( isNaN( y ) ) { y = -1; };
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    if ( isNaN( x ) ) { x = -1; };
    y = parseFloat( y );
    if ( isNaN( y ) ) { y = -1; };
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]
    This way you know for sure that you've got a number to do the numeric comparison, before trying that comparison. What do you think?

    Allan
  • bwlangbwlang Posts: 20Questions: 0Answers: 0
    Yours is cleaner, but it's a different logice.

    My logic says "sort numerics to the top, then ascending or descending)"
    Yours says "sort non-numeric to the top or the bottom depending on sort order".

    I think both are reasonable, but i suspect people don't want to look at blanks so I just put them at the end of the list, this means that the logic has to be a bit more complicated though (or i don't see the simplification).
  • JotheJothe Posts: 1Questions: 0Answers: 0
    adding this condition - if ( isNaN( y ) ) { y = -1; }; - is causing Alerts to popup with a message "NaN" whenever the js function is invoked having this condition. Why is this happening??
This discussion has been closed.