In JavaScript only dots are valid as decimal separator. As DataTables uses JavaScript to sort columns values, it will consider number with comma separator as strings. However, this problem can be easily overtaken with a sort plug-in.
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"numeric-comma-pre": function (a) {
// prepare number
a = +(a.replace(",", "."));
a = (isNaN(a)) ? Number.MAX_VALUE : a;
return a;
},
"numeric-comma-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"numeric-comma-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
The previous code block defines a new data type, to be used by DataTables to sort columns. This code is an adaptation of an example that can be found in DataTables official site.
The first function in the plug-in object, is the one that converts the cell value to number, so that it can be sorted by the following functions. In this case I followed a suggestion from this post, and used the unary operator to convert the value to number. If the value is not a number then NaN is returned, and I change it to Number.MAX_VALUE. I do it this way because I choose to move invalid numbers last when sorting ascending.
After that, only remains create the datatable, and define the new data type for the columns we want.
jQuery("#myTable").dataTable( {
"aoColumnDefs": [
{ "sType": "numeric-comma", "aTargets": [2,3] }
]
});
Supposing that third and fourth columns have comma separated numbers, they now must be sorted correctly.