in reply to Sorting a table
my %users; my %rank_order = ( Leader => 1, Chairman => 2, Lieutenant => 3, Veteran => 4, Rookie => 5, ); sub by_hi_rank { $rank_order{$users{$a}{title}} <=> $rank_order{$users{$b}{title} +} || $a cmp $b } sub by_lo_rank { $rank_order{$users{$b}{title}} <=> $rank_order{$users{$a}{title} +} || $a cmp $b } my %sorters = ( by_rank => { ascending => \&by_hi_rank, descending => \&by_lo_rank, }, by_user => { ascending => sub { $a cmp $b }, descending => sub { $b cmp $a }, }, ); my $sorter = $sorters{$criteria}{$order} or die; my @sorted_users = sort { $sorter->() } keys %users;
|
|---|