Disce aut Discede
Learn or Leave
Add post
Tags : perl sort ips array

$ cat sortip.pl

#!/usr/bin/perl

@ips_unsorted=('127.0.0.1', '1.2.3.4', '9.52.234.76', '10.72.1.255', '240.0.175.62', '192.168.38.1', '6.5.4.3', '10.20.205.1', '192.168.3.81');

@ips_sorted = sort { pack("C4",split(/\./,$a)) cmp pack("C4",split(/\./,$b)) } @ips_unsorted;

print join("\n",@ips_sorted)."\n";

$ ./sortip.pl
1.2.3.4
6.5.4.3
9.52.234.76
10.20.205.1
10.72.1.255
127.0.0.1
192.168.3.81
192.168.38.1
240.0.175.62
$