Numeric Sorting in DataTables

Numeric Sorting in DataTables

eswareswar Posts: 4Questions: 1Answers: 0

hi - I do have numeric values in my tables that is embedded in the <a> tag and the sorting doesn't seem to be working. I do have a String column which is working fine. I have included the debug in the below link -

https://debug.datatables.net/asuluz

Column #7 in this table which is a numeric value in this table with <a> is not getting sorted properly, any help on this would be really helpful. Thank you.

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Hi,

    Interesting - might you be able to link to the page in question so I can debug it directly please? It certainly looks like something has gone wrong somewhere...

    Allan

  • eswareswar Posts: 4Questions: 1Answers: 0
    edited January 2015

    Hi Alan - actually I was able to get to the cause of this problem. I had () to reporesent negative numbers and also asterick's in the numbers. So I used the below script to convert these special chanrcters and it works fine now. thank you!

    jQuery.fn.dataTableExt.oSort['num-html-asc']  = function(a,b) {
        var x = (a == "-") ? -9999999999999999 : a.replace( /<.*?>/g, "" ).replace(/[,*]/g, "").replace( /[(]/g, "-").replace(/[)]/g, "");
    
        var y = (b == "-") ? -9999999999999999 : b.replace( /<.*?>/g, "" ).replace(/[,*]/g, "").replace( /[(]/g, "-").replace(/[)]/g, "");
    
        x = parseFloat(x);    
        if ( isNaN( x ) ) { x = -9999999999999999; };
        y = parseFloat(y);
        if ( isNaN( y ) ) { y = -9999999999999999; };
    
        return ((x < y) ? -1 : ((x > y) ?  1 : 0));
    };
    
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Very nice - thanks for sharing your solution with us.

    Allan

  • eswareswar Posts: 4Questions: 1Answers: 0

    thank you! I am not able to clear the data from the debug link - https://debug.datatables.net/asuluz .. would you be able to delete it for me, I am using the same machine from which I created this but still I don't see the clean data button?

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Certainly - it has been deleted now :-).

    Allan

  • eswareswar Posts: 4Questions: 1Answers: 0

    Thank you!

This discussion has been closed.