Negative value sorting with dollers symbol

Negative value sorting with dollers symbol

rathinashanmugamrathinashanmugam Posts: 1Questions: 1Answers: 0

I have some negative values in my dataset, when convert the negative datatables converts to the value it comes with in the brackets, so the sorting was not work perfectly, I need solution for proper sorting so help me from out of this.

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page, if you can't provide a link to your own page can be found here.

    Thanks,
    Tom

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    After having another look, I believe this should help you out. This should sort negative values using both $(100) and ($100).

    $.fn.dataTable.ext.type.order['currency-pre'] = function ( data ) {
      
        var expression = /((\(\$))|(\$\()/g;
        //Check if its in the proper format
        if(data.match(expression)){
            //It matched - strip out parentheses & any characters we dont want and append - at front      
            data = '-' + data.replace(/[\$\(\),]/g,'');
        }else{
          data = data.replace(/[\$\,]/g,'');
        }
        return parseInt( data, 10 );
    };
    
    
    $(document).ready( function () {
      var table = $('#example').DataTable({
        "columnDefs": [ {
                "type": "currency",
                "targets": 5
            } ]
      });
    
    } );
    
    

    Thanks

    Tom

  • allanallan Posts: 63,799Questions: 1Answers: 10,514 Site admin

    Just to add to what Tom as said, his solution utilises a plug-in sorting method he has created. You need to use columns.type to tell DataTables which column to apply that sorting type to. We'll look at creating an automatic type detection plug-in for it in future :-)

    Allan

This discussion has been closed.