When dealing with your own data formats, it can be desirable to define how you want sorting to occur. Using plug-in sorting functions, you have have DataTables sort data in any manner you wish. You tell DataTables how to sort a column by using the sType parameter - if it is not given then DataTables will attempt to automatically detect the type based on it's built in formatting functions.
This example shows sorting with a comma (',') for a decimal place. This sorting plug-in (and others can be found on DataTables.net.
| Rendering engine | Browser | Platform(s) | Engine version | CSS grade |
|---|---|---|---|---|
| Rendering engine | Browser | Platform(s) | Engine version | CSS grade |
| Gecko | Firefox 1.0 | Win 98+ / OSX.2+ | 1,7 | A |
| Gecko | Firefox 1.5 | Win 98+ / OSX.2+ | 1,8 | A |
| Gecko | Firefox 2.0 | Win 98+ / OSX.2+ | 1,8 | A |
| Gecko | Firefox 3.0 | Win 2k+ / OSX.3+ | 1,9 | A |
| Gecko | Camino 1.0 | OSX.2+ | 1,8 | A |
| Gecko | Camino 1.5 | OSX.3+ | 1,8 | A |
| Gecko | Netscape 7.2 | Win 95+ / Mac OS 8.6-9.2 | 1,7 | A |
| Gecko | Netscape Browser 8 | Win 98SE+ | 1,7 | A |
| Gecko | Netscape Navigator 9 | Win 98+ / OSX.2+ | 1,8 | A |
| Gecko | Mozilla 1.0 | Win 95+ / OSX.1+ | 1 | A |
jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
$('#example').dataTable( {
"aoColumns": [
null,
null,
null,
{ "sType": "numeric-comma" },
null
]
} );
} );
Please refer to the DataTables documentation for full information about it's API properties and methods.