2

I am trying to sort a column with numerical numbers with comma (,).

I am getting a wrong result using the num-fmt option:

enter image description here

Here is my code :

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'num-fmt' }
    ]
});

1 Answer 1

6

Use numeric-comma plugin to sort numbers correctly which use a comma as the decimal place.

Either include //cdn.datatables.net/plug-ins/1.10.11/sorting/numeric-comma.js or use it inline as shown below:

$.extend( $.fn.dataTableExt.oSort, {
    "numeric-comma-pre": function ( a ) {
        var x = (a == "-") ? 0 : a.replace( /,/, "." );
        return parseFloat( x );
    },

    "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));
    }
} );

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'numeric-comma' }
    ]
});
Sign up to request clarification or add additional context in comments.

1 Comment

this replacement doesn't seem to work for 6+ digits where we have more than 1 comma. using a.replace( /,/g, "" ); seems to work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.