my @ips = qw( 127.0.0.1 10.129.30.5 10.129.30.2 255.255.255.255 10.109.30.5 142.222.130.23 10.109.1.1 142.222.87.30 ); my @sorted = map { $_->[1]; } sort { $a->[0] cmp $b->[0] } map { [ sprintf("%03d.%03d.%03d.%03d", split /\./), $_ ] } @ips; print map { $_.$/ } @sorted;