sub mysort { $_[0]->{$a} cmp $_[0]->{$b} } # ... later ... for my $key (sort mysort(\%hash), keys %hash) { # and so on