How do I sort a date of format mm/yy with the jquery tablesorter?
-
03-07-2019 - |
Question
I have a table wherein one of the columns displays the date in the mm/yy format viz. 12/08, 01/09, 02/08, etc.
How do I implementing sorting on this column with the jQuery tablesorter (http://tablesorter.com) so that the dates are appropriately sorted?
Using the default date format does not work as it expects a date of three elements (mm/dd/yy such as 01/06/09).
Solution
See http://tablesorter.com/docs/example-parsers.html for information about custom parsers.
You need to do a text sort but with the year and month switched. You could use a format function similar to this:
format: function(s) {
date = s.split(/\//);
return date[1] + date[0];
},
There may be a simpler way, but that is what I was able to find in my quick search through the documentation.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow