perl中如何将按hash值排序
1、如果是按ASCII码(字符串)排序,则代码如下:
1 foreach my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
2 my $value = $hash{$key}; 3 # do something with ($key, $value) 4 }
2、如果是按数字大小排列,则代码如下:
1 foreach my $key ( sort { $hash{$a} <=> $hash{$b} } keys %hash ) { 2 my $value = $hash{$key}; 3 # do something with ($key, $value) 4 }