goonfest has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I have a 2-dimensional array, whose contents will be displayed in an HTML table.

Each item in the first ROW of the array, represents its respective column name/title.

I am wanting to click on any column heading, and have my script run and sort the array by that column, then re-displaying the results to the webpage.

Can anyone offer any suggestions, or sample code, and/or a URL where I can see this code in action?

Thanks in advance.

Replies are listed 'Best First'.
Re: HTML Table sort
by arturo (Vicar) on Feb 19, 2001 at 23:30 UTC

    Shouldn't be too hard, assuming I understand what you're trying to do.

    What you want, I take it, is for your HTML table to be written out so that it passes a value that identifies the column by which to sort to the CGI. You can do that by putting the column title (or its index #) into the URL of the link.

    As for sorting by that, well, you'll need a way of storing the original indices of the values you're sorting and the final ordering, to generate a mapping (what was in position #1 goes to #3, etc.), so you can apply that map to generate all the other columns. Fun! (One way to do the mapping: use a hash, whose keys are the original indices and whose values are the sorted indices) Somewhat less fun, but also easier, is if you store the information in a relational database and use ORDER BY on the relevant column.

    As for a URL (betcha it ain't implemented in Perl, but) that has code that does what you want, you might try http://www.nhl.com and check the player statistics for any team, it can return them sorted by any category (I'm sure other sports sites do this, but I'm Canadian, so =P )

    HTH,

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: HTML Table sort
by TheoPetersen (Priest) on Feb 19, 2001 at 23:33 UTC
    It's not a short sample, but... Apache::AutoIndex does this. It uses Apache functions for gathering parameters and so on, but wouldn't be hard to translate back to CGI.pm if you prefer.

    The code for the sorting is in do_sort; it's not especially clever, just uses a different sort call for each column selection.

Re: HTML Table sort
by allogenes (Beadle) on Feb 20, 2001 at 00:08 UTC
    Just an additional thought....
    when I don't want to make a trip back to the server,
    I load my data (from an SQL query) into a JavaScript array
    (within my cgi script) and let the client browser
    do all the sorting. Not recommended for all uses,
    but good for some.
    "all was created from /dev/null and shall return to /dev/null"
Re: HTML Table sort
by $CBAS (Scribe) on Feb 20, 2001 at 01:13 UTC

    If you have any sympathy for your servers than maybe you could transfer the sorting to the client using the Tabular Data Control in IE4+. It's a Data Access Object especially for inserting rows into a HTML table and has some filtering/sorting options.
    I don't know the exact link but just search for it on MSDN.

    Of course, you'll still need a solution for non-IE browsers but this is worth looking into (I used it for a pricate search engine and it does a magnificent job ... up until 500 items where my HTML became too heavy and reloading it from a server with my modem would've been faster ;-))

    Enjoy,
    CBAS