for my $i (sort { $hash{$b}{$orderby} <=> $hash{$a}{$orderby} || $hash{$b}{$orderby} cmp $hash{$a}{$orderby} } keys %hash) { # do stuff }