pc0019 has asked for the wisdom of the Perl Monks concerning the following question:
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:
Looks horrible IMO. I wrote the script last night and am having trouble understanding it now.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); } }
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a table
by Fletch (Bishop) on Jul 02, 2008 at 18:13 UTC | |
by pc0019 (Acolyte) on Jul 03, 2008 at 01:11 UTC | |
|
Re: Sorting a table
by ikegami (Patriarch) on Jul 02, 2008 at 19:31 UTC | |
|
Re: Sorting a table
by friedo (Prior) on Jul 02, 2008 at 18:07 UTC |