views:

3232

answers:

1

I am using tablesorter plugin to sort my tables in an MVC .NET App. Most of my columns are strings and I'm having no problems with them. Neither with the numeric ones. The thing is my datetime columns are also getting sorted as if they were strings. They get sorted like this: 01/04/2009, 02/02/2009, 03/08/2009, etc. I obtain the data from the Model in that View.

My call is the default one:

$("#table").tablesorter();

I tried specifying dateformat with no luck:

$("#table").tablesorter({
            dateFormat: 'dd/mm/yyyy'});

The odd thing happens when I manually type a static table with random dates. It gets sorted! But my data comes from a DB call and is put into the Model, I then itreate through it and write tr's with the data.

Thanks in advance.

EDIT: Could it be something related with the way I create the tr's?

<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.date) %>
</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<% } %>
+2  A: 

Try adding a Tablesorter parser to your date column. Tablesorter comes with a parser for shortDate, usLongDate and isoDate.

$("#table").tablesorter({
    headers: { colNum: { sorter: 'shortDate'} }
});

where colNum is the column with the dates. The only example I could find on the tablesorter site is here. This also works if tablesorter is sorting numbers wrong as well. There are other parsers as well for percent, ip address and more. Take a look near the end of the source code and they'll be listed there.

Edit: In looking at the source code, the dateFormat option appears to be looking only for "us", "uk", "dd/mm/yy" or "dd-mm-yy". What happens when you try "uk"?

Ben Koehler
I tried shorDate parser aswell but forgot to mention. No luck either :( I'll give a try with the other date parsers and let you know. Thanks
Gabe G
That worked! I put the call as follows:$("#table").tablesorter({dateFormat: 'uk'});I keep wondering why, with the default call, works as intended when writing a table in plain HTML and not when MVC-generating it (and having to find this workaround).Thanks!
Gabe G