Number sorting fail
Number sorting fail
jkd77
Posts: 3Questions: 0Answers: 0
Please see the following example page: http://lovefunding.werremeyer.com/loan-closings/
Note that when sorting the 'loan amount' column that the largest number, $10,512,700 seems to not sort properly.
Thoughts? Thanks!
Note that when sorting the 'loan amount' column that the largest number, $10,512,700 seems to not sort properly.
Thoughts? Thanks!
This discussion has been closed.
Replies
[code]
// add a "currency" sorting class
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
/* Remove the currency sign */
x = x.substring( 1 );
y = y.substring( 1 );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y;
};
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
/* Remove the currency sign */
x = x.substring( 1 );
y = y.substring( 1 );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return y - x;
};
jQuery(document).ready(function($)
{
$('.loanClosings').dataTable({
"bFilter": false
aoColumnDefs: [ { aTargets: [3], sType: "currency" } ] // use the "currency" sorting class added above, apply to column 3
});
});
[/code]
the code above overrides sort detection and specifies the type to use with sType. it also adds sort functions for that type.
you can find more info and sort plug in types/routines at http://www.datatables.net/plug-ins/sorting