Sort Columns with Numbers and Empty Cells

Sort Columns with Numbers and Empty Cells

josiahsavaryjosiahsavary Posts: 2Questions: 0Answers: 0
edited May 2012 in Plug-ins
I noticed that sorting cells numerically works pretty well unless you have empty cells mixed in with integers in your column. This plug-in makes sure that empty cells get sorted lower than 0.

jQuery.fn.dataTableExt.oSort['numWithNull-asc'] = function(a,b) {
var x = parseInt(a);
var y = parseInt(b);
return ((isNaN(x) || x < y) ? -1 : ((isNaN(y) || x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numWithNull-desc'] = function(a,b) {
var x = parseInt(a);
var y = parseInt(b);
return ((isNaN(x) || x < y) ? 1 : ((isNaN(y) || x > y) ? -1 : 0));
};

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Looks good - thanks for sharing it with us :-)

    Regards,
    Allan
  • josiahsavaryjosiahsavary Posts: 2Questions: 0Answers: 0
    No prob! ;)
  • ImplicatedImplicated Posts: 1Questions: 0Answers: 0
    edited May 2012
    I'm trying to use this (thank you by the way!) and it doesn't seem to be working for me...empty columns are still showing ranked higher than '1'

    I've used the code above exactly as it is and the following to set up the table...
    [code]
    $(document).ready(function(){
    $.extend( $.fn.dataTableExt.oStdClasses, {
    "sWrapper": "dataTables_wrapper form-inline"
    } );

    jQuery.fn.dataTableExt.oSort['numWithNull-asc'] = function(a,b) {
    var x = parseInt(a);
    var y = parseInt(b);
    return ((isNaN(x) || x < y) ? -1 : ((isNaN(y) || x > y) ? 1 : 0));
    };
    jQuery.fn.dataTableExt.oSort['numWithNull-desc'] = function(a,b) {
    var x = parseInt(a);
    var y = parseInt(b);
    return ((isNaN(x) || x < y) ? 1 : ((isNaN(y) || x > y) ? -1 : 0));
    };

    $('#data_table').dataTable({
    "sDom": "<'row'<'span11'f>r>t<'row'<'span3'l><'span8'p>>",
    "sPaginationType": "bootstrap",
    "iDisplayLength": 25,
    "aoColumns": [
    null,
    null,
    null,
    { "sType" : "numWithNull" },
    null,
    { "sType" : "NumericOrBlank" },
    null,
    { "sType" : "NumericOrBlank" },
    null,
    null,
    null
    ]
    });
    });
    [/code]
    As you can see I also tried the NumericOrBlank code posted elsewhere on these forums...which resulted in erratic behavior with the empty cells.

    Any ideas?
  • otuzelotuzel Posts: 1Questions: 0Answers: 0
    I had to sort integers in a column and floats in another.
    I use "?" for null values also. At last I found a method for it. I just changed parseInt to parseFloat for my float values column. Now everything works fine.

    Thank you very much. You saved my day...
  • zlippardzlippard Posts: 1Questions: 0Answers: 0
    edited March 2013
    I ran in to the same problem as Implicated did. Switch the x's and y's in the isNaN functions in each return statement. Swapping the two variables fixed the problem for me.

    Like so:
    [code]return ((isNaN(y) || x < y) ? -1 : ((isNaN(x) || x > y) ? 1 : 0));[/code]
  • joel10joel10 Posts: 3Questions: 0Answers: 0
    edited April 2013
    I have a problem with sorting numeric-comma .. could anyone help?
  • jlunajluna Posts: 2Questions: 0Answers: 0
    Maybe this'll help you

    http://datatables.net/forums/discussion/comment/27376#Comment_27376
This discussion has been closed.