for (@first_sort) { if ( &goes_first( $my_server_hash{$_} )) { push( @topset, $_ ); } elsif ( &goes_second( $my_server_hash{$_} )) { push( @set2, $_ ); # more steps if you want... } else { push( @lastset, $_ ); } } # now go through those subset arrays and print them in turn; # alpha-sorting is preserved within each subset...