my $rHash = \%hash; for my $k1 = (sort keys %$rHash) #outer key, this works { for my $k2 (sort ( {keys %{rHash->{$k1}{$a}} <=> keys %{$rHash->{$k1}{$b}} } keys %{$rHash->{$k1}}) ) { #display miscellaneous print $k1 $k2; } }