in reply to Sort Hash Values Alphabetically
my @keys = sort { $hash{$a} cmp $hash{$b} } keys %hash;
See also sort for how to construct a custom sorting function. Note that since some of your values are duplicates, a sort is not guaranteed stable, i.e. you can get different results from subsequent executions. If this matters, you can add an extra term to your sort so it sorts first by value then by a guaranteed unique quantity, the key:
my @keys = sort { $hash{$a} cmp $hash{$b} or $a cmp $b} keys %hash;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort Hash Values Alphabetically
by danj35 (Sexton) on May 25, 2010 at 14:27 UTC |