in reply to How to sort IP addresses

Have you tried the cmp operator to compare them as strings?
#! /usr/bin/perl use warnings; use strict; use Data::Dumper; my @ips = qw/ 123.12.255.12 12.255.67.19 255.78.132.255 12.255.67.18 12.245.66.20 123.33.65.89 123.32.65.78 255.78.130.123 123.33.65.78 /; my @sorted_ips = sort {$a cmp $b} @ips; print Dumper \@sorted_ips; ---------- $VAR1 = [ '12.245.66.20', '12.255.67.18', '12.255.67.19', '123.12.255.12', '123.32.65.78', '123.33.65.78', '123.33.65.89', '255.78.130.123', '255.78.132.255' ];

Replies are listed 'Best First'.
Re^2: How to sort IP addresses
by GrandFather (Saint) on Jan 29, 2007 at 22:37 UTC
    use strict; use warnings; my @ips = qw(123.156.89.12 12.245.67.1 12.45.67.180 12.145.66.20); @ips = sort {$a cmp $b} @ips; print join "\n", @ips;

    Prints:

    12.145.66.20 12.245.67.1 12.45.67.180 123.156.89.12

    is probably not what OP was after.


    DWIM is Perl's answer to Gödel