I'm using the JQuery tablesorter plugin. The table has a column that shows dates in the format 05 Mar 2012
. The tablesorter plugin seems to treat this column as text, because it sorts it in the order
- 05 Mar 2012
- 06 Jan 2012
- 07 Dec 2012
How can I sort these dates in chronological order instead?
I'm using the JQuery tablesorter plugin. The table has a column that shows dates in the format 05 Mar 2012
. The tablesorter plugin seems to treat this column as text, because it sorts it in the order
- 05 Mar 2012
- 06 Jan 2012
- 07 Dec 2012
How can I sort these dates in chronological order instead?
Share Improve this question asked Mar 5, 2012 at 14:39 DónalDónal 188k177 gold badges586 silver badges844 bronze badges4 Answers
Reset to default 3Parse the date string to a Date, then convert it to milliseconds. Let tablesorter sort the column as numeric.
$.tablesorter.addParser({
id: 'my_date_column',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
var timeInMillis = new Date.parse(s);
return timeInMillis;
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("table").tablesorter({
headers: {
6: { // Change this to your column position
sorter:'my_date_column'
}
}
});
});
If you have trouble with Date.parse, see my answer to this question.
You can also add a hidden span tag before your date in numerical format (yyyymmdd). This text will e first and be used for sorting but it will be hidden from sight and only show the format you want.
<td><span style="display:none">20130923</span>23rd September 2013</td>
You will need to use addParser method and create a parser that converts your string to date object.
follow example on plugin site https://mottie.github.io/tablesorter/docs/example-parsers.html
If need a date parser:
http://www.datejs./
EDIT: your dates convert easily in format shown:
console.log(new Date('05 Mar 2012'))// logs proper date object
Haven't used tablesorter in a while but I seem to remember having a similar issue with UK dates.
You can add a dateformat parameter to the tablesorter plugin with your custom dateformat.
dateFormat: 'dd MMM yyyy'