Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: sort direction

by ph713 (Pilgrim)
on Oct 07, 2005 at 13:09 UTC ( [id://498197]=note: print w/replies, xml ) Need Help??


in reply to sort direction

Try this on for size:

#!/usr/bin/perl -w use Carp; my %people = ( Bob => { age => 60, town => 'Harrisburg' }, Joe => { age => 55, town => 'Calgary' }, Sue => { age => 40, town => 'Houston' }, Jim => { age => 42, town => 'Dresden' }, Ann => { age => 47, town => 'Los Angeles' }, ); my %data_types = ( age => 'numeric', town => 'text', # etc... ); sub sort_people_by { my ($people,$sortby,$dir) = @_; my $dtype = $data_types{$sortby}; if(!$dtype) { croak "No data type defined for $sortby"; } elsif($dtype eq 'numeric') { if($dir eq 'ASC') { return sort { $people->{$a}->{$sortby} <=> $people->{$b}->{$sortby} } keys %$people; } else { return sort { $people->{$b}->{$sortby} <=> $people->{$a}->{$sortby} } keys %$people; } } elsif($dtype eq 'text') { if($dir eq 'ASC') { return sort { $people->{$a}->{$sortby} cmp $people->{$b}->{$sortby} } keys %$people; } else { return sort { $people->{$b}->{$sortby} cmp $people->{$a}->{$sortby} } keys %$people; } } else { croak "Data type $dtype unknown"; } } sub print_people_sorted_by { my ($people,$sortby,$dir) = @_; foreach my $person (sort_people_by($people,$sortby,$dir)) { print "$person - $people{$person}{$sortby}\n"; } } print_people_sorted_by(\%people,'age','ASC'); print_people_sorted_by(\%people,'town','DESC');

Updated style to be closer to PBP, since I need the practice anyways - old habits die hard :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://498197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found