Bug: Sort number column, and sType

Bug: Sort number column, and sType

cu8164kpcu8164kp Posts: 6Questions: 0Answers: 0
edited July 2009 in Bug reports
I'm sorting my columns and my numeric column gets sorted by first digit like so

1
12
130
2
21
22
3
4
5

So i thought to try the aoColumns "sType" and set the column to be numeric. This worked for the default sort, but with that option set when I click the sort arrow it doesn't do anything. Any column I specify with "aoColumns" breaks sorting for that specific column. It didn't matter if it was Date, or numeric if I set the aoColumn sorting breaks for that specified column.

Thanks for any help

I have same problem with 1.4.3 and 1.5beta7.

Thanks for any help.

My example settings:

$(document).ready(function() { $('#table1').dataTable({
"aaSorting": [[ 0, "desc" ]],
"bPaginate": true,
"bFilter": true,
"iDisplayLength": 50,
"aoColumns": [
{ "sType": 'numeric' },
null,
{ "sType": 'date' },
{ "sType": 'numeric' },
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
]
});} );

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi cu8164kp,

    If DataTables is not detecting the numeric type, then it must be because you have non-numeric data somewhere in that column. This might be white-space, or some letters. As you will be able to see from my zero-config example numeric data is correctly detected and sorted: http://datatables.net/examples/example_zero_config.html (fourth column).

    Regards,
    Allan
  • cu8164kpcu8164kp Posts: 6Questions: 0Answers: 0
    You are right. What I had in my table was this:
    {{ object.ticket.id }}

    Switching to the following fixed the issue:
    {{ object.ticket.id }}

    If I remove the tags it sorts correctly, but that takes away from some of the functionality. If I can keep my it allows me to link to the document. Is there any other way I can set a link without messing up the sort type of a column?

    Thanks,
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi cu8164kp,

    Okay got it - thanks for the information! Yes, the HTML would cause DataTables to treat the data as a string, hence the odd sorting, so what you can do is you make use of the "Numbers with HTML" sorting plug-in: http://datatables.net/plug-ins#sorting_numbers_html

    Simply include this code (just after DataTables) and then set the sType for your column to "num-html" and it would work as you would expect!

    Hope this helps!
    Allan
  • njrajunjraju Posts: 4Questions: 0Answers: 0
    Hi cu8164kp,

    How did u get the ur functionality working, even i.m facing the same problem if anchor tabs are present.
  • njrajunjraju Posts: 4Questions: 0Answers: 0
    please help me out ASAP
  • pianobikermanpianobikerman Posts: 1Questions: 0Answers: 0
    THis might help someone, but I was having problems with num-html sorting until I changed my aoColumn defs from sSortDataType - to sType. I would like to know why this change worked? I was pulling my hair out and just trying different things. I started using the bwlang improved version of the num-html sort since I like that logic. http://www.datatables.net/forums/discussion/4456/improvement-to-num-html/p1

    //sets up numeric sorting of links
    jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
    };
  • snoutzsnoutz Posts: 1Questions: 0Answers: 0
    i have same problem like cu8164kp, and what alan said was work for me. just add "Numbers with HTML" sorting plug-in: http://datatables.net/plug-ins#sorting_numbers_html.Simply include this code (just after DataTables) and then set the sType for your column to "num-html" and it would work as you would expect!. note : do not use "num-html-asc" or "num-html-desc" it doens work.
  • ZyyZyy Posts: 2Questions: 0Answers: 0
    Allan,
    what do you mean by
    " include this code (just after DataTables) and then set the sType for your column to "num-html" "
    how to do that?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    See this example: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html .

    Allan
  • PVermaPVerma Posts: 1Questions: 0Answers: 0
    Hi,
    In my data table, i'm showing a column that should do a numeric sort in descending order. But what i'm getting is sth like this. Its showing in descending order, but when no of digits changes its doing like this.

    521003
    521002
    1000004
    1000003
    1000002
    1000001

    I also have some blank values in this column. Can anyone suggest what can i do so that plugin does numeric search properly.

    in the plugin i also added: aoColumns":[{"sType": "num-html"}] as it's the first column to be sorted.
    By doin this css and pagination breaks for the plugin.

    When i remove the aoColumns and add the below functions, it shows the result correctly but the sorting function on column header is screwd for that column.
    "numeric-comma-asc": function(a,b) {alert('numeric-comma-asc');
    var x = (a == "-") ? 0 : a.replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    },

    "numeric-comma-desc": function(a,b) {alert('numeric-comma-desc');
    var x = (a == "-") ? 0 : a.replace( /,/, "." );
    var y = (b == "-") ? 0 : b.replace( /,/, "." );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    }
    ,

    Kindly provide your valuable inputs.

    Thanks
    PVerma.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Can you run your table throughout he debugger ( http://debug.datatables.net ) and post the debug code please?

    Allan
  • goobiegoobie Posts: 2Questions: 0Answers: 0
    edited August 2012
    "
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited August 2012
    > 'bServerSide': true

    Sorting is being done at the server - not the client. So there must be something going wrong in the server-side script. I'd suggest looking into that.

    I should also say that since the sorting is being done at the server, sType has no effect - that is a client-side only parameter.

    Allan
  • goobiegoobie Posts: 2Questions: 0Answers: 0
    edited August 2012
  • ratuzratuz Posts: 1Questions: 0Answers: 0
    edited March 2013
    Hi PVerma:

    the regular expression in both of the sorting functions is only replacing one occurrence of the comma, change every replace ("a" and "b") to something like this
    [code]
    a.replace( /,/g, "" );
    [/code]

    the g at the end is for a global replacement, I had the same issue as you and this fixed mine.

    Hope this helps
  • joel10joel10 Posts: 3Questions: 0Answers: 0
    yo tengo un problema con el sorting numerico, no funciona correctamente.. me podrian ayudar, se los agredecerìa
  • joel10joel10 Posts: 3Questions: 0Answers: 0
    quiero ordenar los numeros de mayor a menor y visceversa
  • jx12345jx12345 Posts: 2Questions: 0Answers: 0
    I've used the html sort plug in and that allows me to successfully sort a column with tags by default (ie. by setting the "aaSorting") but it doesn't seem to allow the column to be reordered by clicking on the column header.

    This returns the following error: Uncaught TypeError: Property 'num-html-asc' of object # is not a function.

    I know a previous post mentions that 'num-html-asc' and doesn't work but does this mean that reordering the column by clicking on the header is not possible with this plugin?

    Many thanks
  • jx12345jx12345 Posts: 2Questions: 0Answers: 0
    ah, just realised you need to include both the type detection plugin:
    http://datatables.net/plug-ins/type-detection

    and the sort plugin:
    http://datatables.net/plug-ins/sorting
This discussion has been closed.