improvement to num-html
improvement to num-html
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]
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]
This discussion has been closed.
Replies
(see http://polbase.neb.com/polymerases )
I did explore using Infinity if one or the other was NaN, but that was cumbersome.
[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
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).