Table Sort JS Library v1.5.4 2003-Aug-1

Supply any <table> with class class="sortable", include the JS library files tablesort.js and virtualpointer.class.js, and place the attribute compareMethod="..." on the desired <th>, and the user will be able to click on the column headers for your table and have the rows sort, client side. NS6+, IE5+ compatible



In Action

Name Age Sex SomeDate Notes
Fred23Male1/4/1973Cool person, really, I like Fred's hair a lot.
Here's an ancillary row of information you may not have known about Fred.
Melissa7Female12/9/1978Big on the cheez whiz.
James12Male3/2/1985Loved and lost, got lost.
Jerome18Male8/8/1967Hits vegas with a vengeance.
Mary33Female2/5/1985Bought 44 .44 automatics and lost them all in the couch.
Susan24Female4/1/1947Bea Arthur thought Susan was the coolest until the pickle incident.
Jerry45Male5/2/1976Signs on the highway pointed Jerry to all the correct exits.
Jim23Male9/3/1684Long since the cricket master.
Samuel14Male3/8/1963Jack and Samuel made some beer, but it tasted bad.
Melissa24Female4/5/2020Taste the ether and you'll go under. Melissa found this out the hard way.
Margaret53Female1/1/1873Point your wings to the west and fly, fly, fly.
Moonbeam43Female3/1/1897Jenny had a vision. Moonbeam was not it.
Sam17Male6/9/1973See note for Samuel. Sam was the taster to discover the truth.
Aaron45Male10/2/1378The Jazz era completely passed Aaron by.
Zachary34Male11/4/1974This boy can jump. Sky. You know?
Zed22Male12/24/1975Of course he's dead. His cricket date went by and he missed it.
Jill68Female2/22/2000Lost in love and I don't know much. But Jill does.
Francess34Female3/19/1991Shortly all will be organized, if Francess has her way.
Jennifer12Female4/13/1967Let's be an honest pair of people about this...who knows what Jennifer was thinking that dark red day?
Daniel51Male8/18/1994Haunting by the closeness of his name to "Danielle", Daniel struggled with his sexuality throughout his life.
Torbjorn6Male4/6/1997Thor's hammer had nothing on the wrath of a Torbjorn enraged.
Thaddeus2Male5/31/1998Quit clowning around and get your work done, boy.
Here's an ancillary row of information you may not have known about Thaddeus!
Joram16Male3/5/1999Van side-swipped a crowd of Bavarian monkeys and yet failed to injure a single one.
Halfdan3Male9/17/2004Belly up to the table and lay down your chips, the time for accounting has come.
Total People: 24
Project Meeting Time
ignore this line and this tbody

Features/Docs

<table ... class="sortable">
This class is required to enable the table to be sortable. Note that the individual th's must also specify a compareMethod (see below) before anything will happen.
<th compareMethod="...">
This attribute is required to make the column sortable. Possible values are text/textual, number/numeric, date, or time.
<table showSortDirection="true">
(optional) Places 5 or 6 at the end of the currently-sorted column header to indicate short direction. IE5Mac has problems with this
<th sortDescending="true">
(optional) Specifies that the intial sort for a column should be in descending rather than ascending order. (Clicking a second time on any sorted column will always switch the sorting method.)
<th defaultSort="true">
(optional) Does an initial sort on the specified column, both ensuring the data is sorted and also (if showSortDirection is on) providing the visual indication of the sorting.
<table ... sortBody="...">
(optional) If your table has multiple tbody tags, use this attribute to specify the id of the tbody you wish to sort. If ommitted, the first tbody (table.tBodies[0]) will be used.
<tr ... ischild="true">
(optional) If there are rows which are associated and should be sorted as a group, put ischild="true" on all <tr> after the first. The group will be sorted according to criteria from the first row, and the child rows will always be kept with the parent.
<table ... getValueFunction="...">
(optional) By default the sort function will compare the innerText of each <td> when deciding how to sort the rows. If you need another way to sort the rows (e.g. a form input, as above, or a custom attribute for the td) then you may supply the name of your own function for retrieving the value. This function will be passed two parameters, [a reference to the table row, and the number of the column being sorted] and should return the value for that row/column pair.
function GetValueFunction_template(tr,colNum){
	return tr.childNodes[colNum].childNodes[0].nodeValue;
}

Notes

Applied Classes
Click Twice to Reverse Sort
Clicking on the <th> for a column which has already been sorted will reverse the sort direction.
date and time compare methods.

When using compareMethod="date", the library calls the JS new Date() function on the value, so oddly-formatted dates may turn into NaN (which is then treated like 0). If you must have a particular date format which does not work, I suggest placing a standard mm/dd/yyyy format as a custom attribute of the TD and then supplying your own getValueFunction such as:

<td sortValue="12/31/2002">Dec-31</td>
...
function GetTDSortValue(tr,colNum){
	return tr.childNodes[colNum].getAttribute('sortValue');
}
Note that since the same function is used for all columns, you will need to use that same custom attribute in every column that you want sortable.

When using compareMethod="time", the library splits the retrieved value around colons (:), and if "pm" exists in the string it will add 12 to the first number. So 1:12am is valid, as is 60:80:23:32

ToDo

FEATURE:Allow Custom getValueFunction per column
Allow individual <th> to have their own getValueFunction, so that columns with a funky value can have custom attributes but standard columns can use the default function.
BUG:IE5Mac & showSortDirection
The rendering engine in IE5Mac has problems with showSortDirection. If the waning IEMac population is important to you, avoid using this attribute.
FEATURE:Multiple Sort Columns
Not sure what the UI would be for it (shift-click to provide backup, use previously sorted column as backup?), but allow sorting by a primary and then secondary column. (Comparison function calls itself again in the 0 case.) Low-priority.