my $tmp; # to avoid allocation every loop iteration @sorted = sort { ($tmp = maturity_sort($the_data{$a}, $the_data{$b})) ? $tmp : ($the_data{$a}->{rich_cheap} <=> $the_data{$b}->{rich_cheap}); } (keys %the_data);