Jules Jules - 10 months ago 59
Javascript Question

Sorting table by column

I'm looking for a way to sort an HTML table; I've never had to do this before, so I don't know what the best way to do so is; in particular, I'm looking for something that's fast, and easy to set up and manage later on. Does anyone know of a jQuery or JavaScript to do so?

Answer Source

You won't get far using JS' sort(). I tend to use the TableSorter plugin for jQuery, which is fairly easy to get going, and even easier if you've already got jQuery loaded.

It can automatically pick up how to sort certain data (strings, ints, etc), but for more complex data (e.g. unusually formatted dates), you may need to write a little extra code to help it along. More info at: http://tablesorter.com/docs/

I think it's also possible to style the sorted tables nicely - the ones on tablesorter.com are rather drab...

Good luck!