Any number
Sort column with mixed numerical content by number
- Author: [david konrad](davidkonrad at gmail com)
Sorts columns by any number, ignoring text. This plugin is useful if you have mixed content in a column, but still want to sort by numbers. Any number means
- integers, like 42
- decimal numbers, like 42.42 / 42,42
- signed numbers, like -42.42 / +42.42
- scientific numbers, like 42.42e+10
- illegal numbers, like 042, which is considered as 42,
- currency numbers, like €42,00
Plain text is ignored; columns with no recognizable numerical content is pushed to the bottom of the table, both ascending and descending.
Plug-in code
_anyNumberSort = function(a, b, high) {
var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;
a = a.replace(',','.').match(reg);
a = a !== null ? parseFloat(a[0]) : high;
b = b.replace(',','.').match(reg);
b = b !== null ? parseFloat(b[0]) : high;
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"any-number-asc": function (a, b) {
return _anyNumberSort(a, b, Number.POSITIVE_INFINITY);
},
"any-number-desc": function (a, b) {
return _anyNumberSort(a, b, Number.NEGATIVE_INFINITY) * -1;
}
});
CDN
This plug-in is available on the DataTables CDN:
Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.
Version control
If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!
- This plug-in: any-number.js
- Full DataTables plug-ins repository: DataTables/Plugins
Example
$('#example').dataTable( {
columnDefs: [
{ type: 'any-number', targets : 0 }
]
} );