in reply to Sorting a Hash by the presence of a $tring Argument

Not sure if I understood your problem correctly, but here's the solution to what I understood:

@RE_QUERY = sort { for my $i (3, 2) { return $QUERY{$a}[$i] cmp $QUERY{$b}[$i] if defined $QUERY{$a}[$i] && defined $QUERY{$b}[$i] && $QUERY{$a}[$i] cmp $QUERY{$b}[$i]; } return $QUERY{$a}[0] cmp $QUERY{$b}[0]; } keys % QUERY;
pike