$hash{A}{B}{C}{D}{one} = "1"; $hash{A}{B}{C}{D}{two} = "2"; $hash{A}{B}{C}{D}{thr} = "3"; $hash{A}{B}{C}{E}{one} = "4"; $hash{A}{B}{C}{E}{two} = "5"; $hash{A}{B}{C}{E}{thr} = "6"; my @sort; push @sort, [ $_, 'D' ] for keys %{$hash{A}{B}{C}{D}}; push @sort, [ $_, 'E' ] for keys %{$hash{A}{B}{C}{E}}; for my $ref ( sort { $a->[0] cmp $b->[0] } @sort ) { print "Key: $ref->[0]; Hash: $ref->[1]\n"; } __DATA__ Key: one; Hash: D Key: one; Hash: E Key: thr; Hash: D Key: thr; Hash: E Key: two; Hash: D Key: two; Hash: E