Tablesorter, do not sort by date correctly

Does tableorter do differently if there is a space?

I am new to tablesorter, but I was quickly offered to add it to the customers site (in particular, so that they sorted by publication date). As far as I know, a date (for example, May 14, 2012) is read as text, so I need to add a parser to change it to numeric so that it is sorted correctly. (e.g. 14052012)

This is me (currently using code written by another developer), located in tablesorter.js).

$.tablesorter.addParser({
id: 'dayMonthYear',
is: function(s) {
    return false;
},
format: function(s) {           

    var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
    var day = String(date[1]);
    if (day.length == 1) {
        day = "0" + day;
    }        

    var month = monthNames[date[2]];
    var year = date[3];
    var sortableDate = '' + year + month + day;
    return sortableDate;
},
type: 'numeric'  
});

var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

I also posted this on the html page

$("table#searchresults").tablesorter ({
    headers: {
        0: {sorter:'dayMonthYear'}
    }
});

Although I saw that this code works, it doesn't seem to work for me. The month is still read as text.

? , , - , , ?


EDIT: , http://beausmith.com/blog/custom-date-sorting-for-jquery-tablesorter-plugin/, . Hopefuly ...

+3
1

. phew

$.tablesorter.addParser({
id: 'dayMonthYear',
is: function(s) {
    return false;
},
format: function(s) {       

    s = $.trim(s.replace(/\s+/g, ' '));


    var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
    var day = String(date[1]);
    if (day.length == 1) { day = "0" + day;}        
    var month = monthNames[date[2]];
    var year = date[3];

    return sortableDate = '' + year + month + day;
},
type: 'numeric'  
});
var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";
+3

All Articles