Sorting plugin

Sorting plugin

sronsieksronsiek Posts: 52Questions: 11Answers: 0
edited March 2012 in Plug-ins
I've been using the sort plugin providing 'num-html' sort, ie
stripping html, then numerically sorting on the remaining value.

Works fine until you have a non-numeric item such as 'N/A' or
'-' or whatever. I made a small adjustment with which non-numerics
will be grouped before / after numerics (asc/desc). This is enough
for my purposes, but I'm sure there's room for improvement ...

Just thought I'd share ... perhaps something like this couls be added to the plugin.

[code]
// http://datatables.net/plug-ins/sorting
// Has more such goodies that can be added as needed.

// Provide numeric sorting after html-tags have been stripped.
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 );
if ( isNaN(x) ) x = -1;
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 );
y = parseFloat( y );
if ( isNaN(x) ) x = -1;
if ( isNaN(y) ) y = -1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
This discussion has been closed.