I have a table, with about 50 rows and 9 columns. Some of the columns contain words, others just numbers.

What is the best way of displaying this data in a web page, where I can sort all the data by clicking on the column header? Ideally, clicking once on the name of the column would sort ascending, and again would sort descending. The same way programs, like WMP or Explorer, work.

If there is a simple way to do the sorting that would be great, if not, I have done that in a horribly long section of code (which I would prefer to get rid of). The main problem is getting the data to sort ascending AND descending by clicking on the column name.

One column needs to be sorted in an order I will define, not alphabetically.
ie: name1, name2, name3, name4
sorts to: name 2, name 4, name 3, name1
(and then backwards for descending)

Again, I have done this already but my method is ugly and long. This is for the ranks, as below. Each &display_rank scans through the whole list of people (~50) so isn't very efficient.

I am using 'CGI qw/:standard/', so ideas ideally in that format, but that's not necessary.


Here is my sorting function:

sub show { my ($order, $option) = @_; if ($option eq 'rank_title' && $order eq 'ascending') { &display_rank('Leader'); &display_rank('Chairman'); &display_rank('Lieutenant'); &display_rank('Veteran'); &display_rank('Rookie'); } elsif ($option eq 'rank_title' && $order eq 'descending') { &display_rank('Rookie'); &display_rank('Veteran'); &display_rank('Lieutenant'); &display_rank('Chairman'); &display_rank('Leader'); } elsif ($option eq 'username') { my $user; if ($option eq 'ascending') { $hoa{$user}{$a} <=> $hoa{$user}{$b}; } elsif ($option eq 'descending') { $hoa{$user}{$b} <=> $hoa{$user}{$a}; } } else { &display($order, $option); } }
Looks horrible IMO. I wrote the script last night and am having trouble understanding it now.

Oh and the data structure is as follows:

A 2D hash contains all data. %hoa
Each element (is that the correct term?) is the name of the user and is another hash. $hoa{$user}
Each of these hashes contains 8 elements. $hoa{$user}{$option}

The data needs to be sorted by any of the 8 elements and the user element.

Any help or ideas would be greatly appreciated!


In reply to Sorting a table by pc0019

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.