Email conversation
From | Jake Hawkes |
To | Me |
Subject | Sortable tables library would be nice |
Date | 30 June 2005 20:29 |
I was just looking around your site for help with a Safari browser
nonconformity when I came across your page comparing browser speeds and
realized you use sortable tables there. You really should turn that into a
library as I think it would be useful to a lot of people.
On a recent project I needed a script to create sortable tables, but after
trying several and tweaking them a whole lot, I still couldn't get them to
work. Anyway, this is just a suggestion, I'm not asking for any help.
Now I'm off to figure out how you do it and copy your method.
Jake Hawkes
From | Me |
To | Jake Hawkes |
Subject | Re: Sortable tables library would be nice |
Date | 30 June 2005 21:53 |
Jake,
> sortable tables there. You really should turn that into a library
Maybe, but it might take a lot of work to make it generic
> Now I'm off to figure out how you do it and copy your method.
The algorithm is fairly simple;
* make an array, and fill it with a reference to every non-header row in the
table.
* sort using a custom sort function
* append each row in turn into the end of the tbody (in the order they are
now in the array)
the sort function must do either one of these two things:
1) numeric sort
* get the parseFloat values of the required cell content
* return one minus the other (and make sure you compensate for NaN values,
so that they always appear at the end)
2) lexical (text) sort
* get the text content of the required cells
* pad out the end of the strings so they are the same length (lexical
sorting compares from the left side, but aligns the characters from the
right edge, so for normal response, they should start the same length) -
make sure the padding character appears earlier in the character set than
any characters in the strings
* return a 1 or -1 value if one is greater than the other
And of course, swap around which one should be greater to use reverse order.
To tell the sort function what type of search to perform, and what cell to
use, I set global variables before calling the sort method, and I check for
those global variables in the sort function.
As you can see, it is fairly hard to make that generic without knowing the
exact tables that will be used, and the type of sort, and column to sort on.
And for a generic one, it should also be able to sort horizontally - that
could be much harder since there is no vertical equivalent to a table row...
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
From | Me |
To | Jake Hawkes |
Subject | Re: Sortable tables library would be nice |
Date | 2 July 2005 01:05 |
Jake,
> realized you use sortable tables there. You really should turn that into
> a library as I think it would be useful to a lot of people.
http://www.howtocreate.co.uk/tutorials/jsexamples/tableSortDemo.html
What can I say? - so you got me thinking ;)
Tarquin