if ($joe eq "numbers") {my $sortype='<=>';} if ($joe eq "letters") {my $sortype='cmp';} #then go on to work on the hash foreach $key (sort { $hoh{$a}->{value} $sortype $hoh{$b}->{value} } keys %hoa) { blah; }