use Sort::Key qw(ikeysort); my @sorted_keys = ikeysort { length ( (%{$HoHoA{$_}})[0] ) } keys %HoHoA;