foreach my $key (sort { substr($hash{$a}, -2) cmp substr($hash{$b}, -2) } keys %hash) { print $key . ' ' . $hash{$key} . "\n"; }