sub num_or_str { no warnings 'numeric'; $_[0] <=> $_[1] || $_[0] cmp $_[1] } my @sorted = sort num_or_str( $people{$a}->{age}, $people{$b}->{age} ), keys %people; #### my @stuff = qw/ 9 -1 -3 hi 0 bye 5 10 /; my $descending = 1; my @ar; if ( $descending ) { @ar = sort { num_or_str($b, $a) } @stuff; } else { @ar = sort { num_or_str($a, $b) } @stuff; } #### my $sort_by = 'age'; my $descending = 1; my $database = \%people; # return an ordered list of copies of keys of a # hash structured as described somewhere # usage: sort_people( \%sort_me, $by_me, $descending ) # Ascending sort is the default. # If $by_me is not a key that exists then ... # Maybe an arref should be returned. sub sort_people { my ( $people, $by, $desc) = @_; no warnings qw/ numeric uninitialized/; if ( $desc) { return sort { your_conditions( $b, $a)} keys %$people; } else { return sort { your_conditions( $a, $b)} keys %$people; } } #### my @sorted_keys = sort_people( \%people, $sort_on, $descending);