in reply to How to sort IP addresses

The secret is to use a sort function. Er, my mistake, you're using one already ... you just need to do a few more comparisons. Perl doesn't have a built in octet comparator for some reason. I use the following little guy a lot, so I just cut and pasted it here. It may help. I call it my ipsort_pipet.

my @lines = <STDIN>; print $_ for sort cmp_ip @lines; sub cmp_ip { my @c = split /\./, $a; my @d = split /\./, $b; for(0.. 3) { return $c[$_] <=> $d[$_] if $c[$_] != $d[$_] } return $a cmp $b }

-Paul

Replies are listed 'Best First'.
Re^2: How to sort IP addresses
by mozerd (Initiate) on Apr 28, 2018 at 11:53 UTC

    -Paul
    Thank you very much for your excellent sort ip code --- did the trick for me in a project I was working on

    -David