Richie
Richie

Reputation: 5199

jquery tablesorter how to sort time format 2014-07-11-02.10.03

I'm trying to use the Jquery tablesorter plugin to order a time like this...

2014-07-11-02.10.03

so that is...

yyyy-MM-dd-HH.mi.ss

I've tried to follow a previous post (date Sorting Problem with Jquery Tablesorter) and do the following....

$.tablesorter.addParser({ 
    id: "customDate",
    is: function(s) {
        return /\d{1,4}-\d{1,2}-\d{1,2}-\d{1,2}-\d{1,2}.\d{1,2}\.\d+/.test(s);
    },
    format: function(s) {
        s = s.replace(/\-/g," ");
        s = s.replace(/:/g," ");
        s = s.replace(/\./g," ");
        s = s.split(" ");
        return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime()+parseInt(s[6]));
    },
    type: "numeric"
});

$("#tabHistoryBackups").tablesorter({
    sortList: [[3,1],[0,0]], 
    stripeRowsOnStartUp: true
});

But it does not work and I am having trouble understanding where I might me going wrong. Could someone help me with this please and tell me what is wrong with what I have? Here is what my table looks like.

enter image description here

thanks

Upvotes: 0

Views: 140

Answers (1)

pete
pete

Reputation: 25091

Your datetime value does not contain milliseconds, so you need to update the RegEx accordingly:

is: function(s) {
    return /\d{1,4}-\d{1,2}-\d{1,2}-\d{1,2}\.d{1,2}\.\d{1,2}/.test(s);
},

You also might note that the . has been escaped (\.) to mean a literal .. This is because, unless it's been escaped or made part of a character class, . is a RegEx metacharacter that means "anything".

You'll also want to update the format method to ignore milliseconds, since your values do not contain them:

format: function(s) {
    s = s.replace(/\-/g, " ");
    s = s.replace(/\./g, " ");
    s = s.split(" ");
    return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime());
}

Upvotes: 1

Related Questions