Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: sorting ip octets

by eclark (Scribe)
on Feb 16, 2005 at 21:29 UTC ( #431762=note: print w/replies, xml ) Need Help??

in reply to sorting ip octets

Pack it!

sort { unpack('N',pack('C4',split(/\./,$a))) <=> unpack('N',pack('C4',split(/\./,$b))) } @list

Replies are listed 'Best First'.
Re^2: sorting ip octets
by eclark (Scribe) on Feb 16, 2005 at 21:34 UTC

    If you dont like using pack. Or you have another type of data in the future, remember than cmp and <=> return 0 if they are the same. See below.

    sort { my @a = split(/\./, $a); my @b = split(/\./, $b); $a[0] <=> $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2] || $a[3] <=> $b[3] } @list

      You should read the FMTYWTK about sort...

      Doing it this way will be really slow on large data sets since you are doing lots of splits...

      Note, if you apply the FMTYWTK technique then you get about the same as my post...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://431762]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-05-25 14:09 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.