Sorting by formatted date in jquery

I am trying to sort a table that has a column like 01 Apr 2010. I am using below to apply sort

$(document).ready(function() 
{ 
    $("#dataTable").tablesorter();  
});

      

But it doesn't work for dd MMM yy format dates. Can anyone suggest how I can apply this format for sorting?

+2


a source to share


1 answer


Check out the parser example page , it will show you how to create your own parser. You can play such dates with new Date(date)

or Date.parse(date)

. I don't have the means to test it, but something like this should work:

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'ddMMMyy', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        // parse the string as a date and return it as a number 
        return +new Date(s);
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

      



All you have to do is specify the sorting method using a parameter headers

:

$(document).ready(function() { 
    $("dataTable").tablesorter({ 
        headers: { 
            6: { // <-- replace 6 with the zero-based index of your column
                sorter:'ddMMMyy' 
            } 
        } 
    }); 
});  

      

+1


a source







All Articles