use Net::IP qw/ ip_iptobin /; my @keys = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ip_iptobin($temphash{$_}->{IP}), $_] } keys %temphash;