foreach $key (sort { ($a) <=> ($b) } keys %hash) { #block of code to operate on %hash; } #### foreach $key (sort{($a)<=>($b)} keys %hash)