sort numbers with the format #,###,###.##

sort numbers with the format #,###,###.##

mmilanmmilan Posts: 5Questions: 0Answers: 0
edited December 2013 in General
In my country the convention for expressing numbers is to use the . as a thousands separator and the , as the decimal separator. an example of this would be: 25.367,212 I have been unable to make datatables.js sort any column that uses this formatting.

I am using the following extensions:

[code]
//formatted number sort
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function (a) {
a = (a === "-" || a === "") ? 0 : a.replace(/[^\d\-\.]/g, "");
return parseFloat(a);
},

"formatted-num-asc": function (a, b) {
return a - b;
},

"formatted-num-desc": function (a, b) {
return b - a;
}
});

//formatted number autodetection
jQuery.fn.dataTableExt.aTypes.unshift(
function (sData) {
var deformatted = sData.replace(/[^\d\-\.\/a-zA-Z]/g, '');
if ($.isNumeric(deformatted) || deformatted === "-") {
return 'formatted-num';
}
return null;
}
);
[/code]


has anyone been able to solve tis problem?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Sounds like you want to use a regular expresso to remove the `.` characters and replace the `,` with a `.` .

    Allan
  • mmilanmmilan Posts: 5Questions: 0Answers: 0
    Im not suere exactly where I could include the regular expression for it to work,

    im guessing it would replace this part:

    a.replace(/[^\d\-\.]/g, "")
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Have a lok at how this plug-in operates: http://datatables.net/plug-ins/sorting#numeric_comma

    Allan
  • mmilanmmilan Posts: 5Questions: 0Answers: 0
    If anyone needs to know how I solved the problem. I have detailed the solution in stackoverflow.

    http://stackoverflow.com/questions/20336091/sort-numbers-with-the-format-with-datatables-js
This discussion has been closed.