How do I use tablesorter so as to sort a table based on column that has relative dates. E.g.: I have something like:
2 days ago
5 days ago
10 days ago
1 weeks ago
3 weeks ago
And current sorting gives:
10 days ago
1 week ago
2 days ago
3 weeks ago
5 days ago
Desired Output:
2 days ago
5 days ago
10 days ago
1 week ago
3 weeks ago
PS: I am new to jQuery.
You can use one of the data libraries, like sugar or datejs.
This demo uses the sugar library to sort a column with that format.
/*! Sugar (https://sugarjs.com/docs/#/DateParsing)
* demo: http://jsfiddle.net/Mottie/7z0ss5xn/
*/
$.tablesorter.addParser({
id: "sugar",
is: function() {
return false;
},
format: function(s) {
// Add support for sugar v2.0+
var create = Date.create || Sugar.Date.create,
date = create ? create(s) : s ? new Date(s) : s;
return date instanceof Date && isFinite(date) ? date.getTime() : s;
},
type: "numeric"
});
/*! Datejs (http://www.datejs.com/)
* demo: http://jsfiddle.net/Mottie/zge0L2u6/
*/
$.tablesorter.addParser({
id: "datejs",
is: function() {
return false;
},
format: function(s) {
var date = Date.parse ? Date.parse(s) : s ? new Date(s) : s;
return date instanceof Date && isFinite(date) ? date.getTime() : s;
},
type: "numeric"
});