1

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?

4

3 回答 3

11
my @sorted = sort { $a cmp $b } keys %hash;

要不就

my @sorted = sort keys %hash;
于 2013-05-23T15:11:54.067 回答
3

哈希键只是字符串:

@sorted = sort keys %hash;
于 2013-05-23T15:11:37.740 回答
1
foreach my $key ( sort {$a cmp $b} keys %hash) {

# do something ..

}
于 2013-05-23T15:17:28.277 回答