SortTable containing plus and minus sign

SortTable containing plus and minus sign

shanisonshanison Posts: 7Questions: 0Answers: 0
edited September 2010 in General
Hi, i have a table contains data that has + sign if it is a positive data and - sign if it is a negative, but when i sort the table it is not displaying correctly. e.g. after sort the column becomes the following:
-0.03
-0.025
+0.19
+0.11
Is there anyway to work around this? Thank you very much

Replies

  • mbjorkembjorke Posts: 5Questions: 0Answers: 0
    I have the same problem. /marcus
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I've just put together a little sorting plug-in which will do the trick for you: http://datatables.net/plug-ins/sorting#Signed_Numbers

    Use it something like this:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumnDefs": [
    { "sType": "signed-num", "aTargets": [ 3 ] }
    ]
    } );
    } );
    [/code]
    Allan
  • mbjorkembjorke Posts: 5Questions: 0Answers: 0
    Thanks for fast feedback Allan. But I'm not sure it will help in my case. I've got a table with financial data with plus, minus, percentage and whitespace. Here are some exampels that didn't work:
    125 000,00 (whitscpace between numers failed)
    +3,59% (percentage problem)
    -3,59% (percentage problem)
    +3,55 (plus char problem)
    -2,55 (minus char problem)

    I was already using the dataTables.numericComma.js plugin which I got some help to rewrite a little using a regular expression instead:

    Here is the script changes I use now that works for me:
    [code]
    jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( /,/, "." ).replace( /[^\d\-]/g, "" );
    var y = (b == "-") ? 0 : b.replace( /,/, "." ).replace( /[^\d\-]/g, "" );
    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( /,/, "." ).replace( /[^\d\-]/g, "" );
    var y = (b == "-") ? 0 : b.replace( /,/, "." ).replace( /[^\d\-]/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };
    [/code]
This discussion has been closed.