Reputation: 4084
I want to make an array that stores an alphabetized list of hash keys. I tried this method:
@sorted = sort { $hash{$a} cmp $hash{$b} } keys %hash;
...But it turns out this returns a list of hash keys sorted by value (whereas I want a list of hash keys sorted alphabetically).
Any suggestions?
Upvotes: 1
Views: 1331
Reputation: 77095
foreach my $key ( sort {$a cmp $b} keys %hash) {
# do something ..
}
Upvotes: 1
Reputation: 385657
my @sorted = sort { $a cmp $b } keys %hash;
or just
my @sorted = sort keys %hash;
Upvotes: 11