my @sortedKeys = sort {$ipaddr {$a} cmp $ipaddr {$b}} keys %ipaddr; ... print OUT "$ipaddr{$_}\t$_\n" for @sortedKeys;