JQuery: TableSorter-Date with a specific format does not work

I use the Tablesorter plugin to sort the table. the fourth column is the date fields in the format:

-> January 30, 2013

-> February 01, 2013

when I try to sort the format, it causes the wrong sort.

My View Page: (one of the date columns)

<td onclick="viewTrainingeDetails(${privateTrainingInstance?.id})"><g:formatDate format="dd MMM yyyy" date="${privateTrainingInstance?.startDate}" /></td> 

JQuery

  $(function() { $("#myTable").tablesorter(); }); 
+4
source share
1 answer

Try adding this custom parser ( demo ):

 $.tablesorter.addParser({ id: "date", is: function (s) { return false; }, format: function (s, table) { return new Date(s).getTime() || ''; }, type: "numeric" }); 

then initialize the plugin as follows:

 $('table').tablesorter({ headers: { 5: { sorter: 'date' } } }); 

Update: for best results, make sure you return a valid date:

 $.tablesorter.addParser({ id: "date", is: function (s) { return false; }, format: function (s, table) { var date = new Date(s); return date instanceof Date && isFinite(date) ? date.getTime() : ''; }, type: "numeric" }); 
+13
source

All Articles