这不是我填充哈希表的方式。为了更加易读,这里是它的内容。其中,键位于一个固定长度的字符串上:
我希望基于值获取已排序的键,所以我的期望是:
my %country_hash = (
"001 Sample Name New Zealand" => "NEW ZEALAND",
"002 Samp2 Nam2 Zimbabwe " => "ZIMBABWE",
"003 SSS NNN Australia " => "AUSTRALIA",
"004 John Sample Philippines" => "PHILIPPINES,
);
我希望基于值获取已排序的键,所以我的期望是:
"003 SSS NNN Australia "
"001 Sample Name New Zealand"
"004 John Sample Philippines"
"002 Samp2 Nam2 Zimbabwe "
我做了什么:
foreach my $line( sort {$country_hash{$a} <=> $country_hash{$b} or $a cmp $b} keys %country_hash ){
print "$line\n";
}
另外:
(我怀疑这样排序不会起作用,但无论如何)my @sorted = sort { $country_hash{$a} <=> $country_hash{$b} } keys %country_hash;
foreach my $line(@sorted){
print "$line\n";
}
它们两个都没有正确排序。我希望有人可以帮助。