views:

48

answers:

4

I have an HTML table with about 100 rows and 3 columns. I have onclick events attached to all 3 of my column headers. What must I do in Javascript to easily sort the table by that column? I can think of a fully blown hand-written approach, but I'm not sure if there are any built-in functions for such a seemingly common operation.

If I have to do this by hand, I'd probably sort the array I used to generate the table in the first place, and simply re-create the table after the sort. Any advice?

+1  A: 

Why reinvent the wheel?

Amber
A: 

There are no built-in functions for this, since it's ultimately DOM manipulation. Sorting the array and re-generating the table sounds ideal.

VoteyDisciple
+1  A: 

Why reinvent the wheel? :)

You can use the tablesorter plugin. It does exactly what you need!

Lorenzo
You make it sound as though jQuery is part of javascript.
Sean Hogan
what make you thinking that?
Lorenzo
A: 

There isn't a built in function, but its been done before, lots! Try TableSorter

Paul Creasey