A blog by Stuart Langridge
Sorttable, the JavaScript table sorting library, is pretty much the most popular thing I’ve ever written. (Top hit for “javascript sort table” on Google, too.) Over the four years or so since I first released it, there’s been a steady trickle of thanks, feature requests, and bug fixes to it. I’ve finally got around to releasing a new version of the script that hopefully covers everything that people were looking for. You can get it at the same place as it ever was, sorttable: Make all your tables sortable.
The most important thing that’s changed is that sorttable now has a much
much clever automatic column typer. You still can just drop sorttable in
place and not have to do any configuration, but it’s quite a bit
cleverer about working out whether your columns are numeric or dates or
currency or text. It skips blank lines, it can tell the difference
between English and American format dates, it understands numbers with
commas in and negative numbers. This is the number one feature request.
Secondly, you can now use custom sort keys for those columns that
sorttable can’t work out for itself. See the page for details, but this
should allow anyone to make anything work with sorttable v2. It’s also a
bit more compliant with HTML standards. To add a “totals” row you should
add the row to a <tfoot>
section in your table. Sorttable v1 used a
class of “sortbottom” on totals rows; for backwards compatibility this
is still supported, but you really should be using <tfoot>
instead. In
a similar way, sorttable now uses <thead>
as your column headers if
you have it. If you do not have it then it will create a <thead>
and
put the first row of your table in it. This is a backwards
incompatible change: to apply style to your table headers, you should
now style table.sortable thead
, not table.sortable a.sortheader
as
in v1. Input elements are now supported in table cells! If your table
cells contain input elements then sorttable will use the value in the
input element. Sorttable is now quite a lot faster than it used to be,
owing to various little tweaks. It’s especially fast at resorting the
table by the column you’ve already sorted by (but reversed), because now
it just reverses the table rather than doing the sort. Stable sorting is
supported but commented out in the code (because the stable sort is
implemented in JavaScript, not using the native list sorter, and so it’s
a lot slower). You can change this by editing one line in sorttable.js
if stable sorting is important; see the page for details.
Lots of people (really, really lots) have contributed code suggestions, feature requests, bug reports, and compliments about sorttable. Here’s my chance to say thankyou. So: thankyou to… Eric Chang, Mehmet Kut, Dan Synder, Jeff Bradley, Eric Holstege, Victor Kryukov, Nikola Ivanov, Geoff Clevenger, Mikko Suniala, Scott Reynen, Justin Meyer, Leonardo Kenji Shikida, Ben Ostrowsky, Daniel Berlin, Richard Snazell, Mark James, Eric Rizzo, Garve, Andreas Rehm, Scott Kingdon, Marcus Månsson, Bill Barry, Michael Rumpler, Avi Rogers, Kevin Wu, Espen Hovlands, Petronel Laviniu Malutan, John M. Pierre, opus27, Brendan Bowles, Marty O’Brien, Alex Rubin, Chris Gay, Jack Vanhan, Guy Rosen, John Dawson, Ryan Korczykowski, Carl Forde, Pete K, Glenn Haser, Richard Grassy, Olivier Briat, Bob Hanson, Alex Z, Narayana Rao Kankipati, Rey Hernandez, Eric Moriztz, Marco Valk, Marcus Daniel, David Dockhorn, Angela Weise, Aneel Nazareth, Jeremy Hulford, Thomas K. Weinstock, Edward, Matthew Egbers, Anton Berezin, Moustafa Elqabbany, Justin Williams, Daniel Chace, Inigo Surguy, Vladimir Kornea, John Mund, Pablo, David Golden, Brad Kemper, Brian Schott, Christian Robottom Reis, Justin Haygood, C. David Eagle, Simon Willison, and Dean Edwards. Phew! All those people have contacted me asking for new features or have sent me patches or have provided code that I was able to reuse in v2 of sorttable. Thankyou: for those of you who wanted sorttable to do something new, I hope you like it.