Net::IP, Net::IP::Resolver, Net::IP::Match::Regexp and other Submodules from Net::IP
are doing that fine for you. Just the part with sorting is difficult. But if you google it, you find some nice Methods. For example:
my @ips = qw(
172.27.32.200
172.19.32.100
10.1.1.60
192.20.30.133
);
@ips = map {s/\s+//g; $_} sort map {s/(\d+)/sprintf "%3s", $1/eg; $_} @ips;
print join "\n", @ips;
Found here