Mon said:
Can the cells in a 2-column table of 307 rows be re-sorted in situ
(i.e., by javascript), based on data within each cell where that data is
always in a certain line and position within that line. ?
Rows and columns of tables, although indexable, are not arrays, and have
no sort method.
Since the elements can be both read and written, all normal sort
algorithms (which does not imply sort methods) can be programmed to work
on tables in situ.
I have an instinctive feeling that handling table elements may be slower
than handling elements of a corresponding array. If speed matters,
check that.
The built-in sort method is likely to be faster than a home-brew, if
given a well-considered sort function or if not needing one.
If you have to sore something unhelpful, such as US-format date strings
or dates containing letters, every comparison will need two of those to
be rendered comparable, and sorting tends to take N log N or more
comparisons. Therefore, in such cases, do a preliminary pass to
generate directly comparable sort keys.
Assume that rows are to be stable, and sorted into the order of a
particular column. I think that if you represent the entire structure
as an array of rows, with the rows being arrays, with the zero element
of a row holding the sort key, and the rest of the array holding the
table data however convenient, then you can use the built-in sort after
setting the keys to suit the order desired.
The art of sorting lies in not moving the actual data, just adjusting
what are in effect pointers. That's easy in JavaScript.
Be aware that there is no need to sort on demand. The page can be
fetched containing, for example, an "2-D" array of rows, and other
simple arrays indexing the data in all orders on offer. Then, when the
user "demands" a sort, just write the table in the desired order using
the appropriate one of those arrays.
If the user can input data, so that it cannot all be pre-sorted at
download, you can insert a pointer to the new data at the right position
of each list while the user is looking for the next data line.
You can also sort all the downloaded data in JavaScript onload, with the
sort buttons temporarily disabled, distracting the user until the
buttons can be enabled,
<
http://www.merlyn.demon.co.uk/js-order.htm> might help.