I was actually here to look for some IPV6 sorting code, but reading various IPV4 sorting solutions made me want to add mine from 10 years ago (when I retired). I've forgotten much, but I remember back then being proud of my fairly simple and fast sort of IP addresses. I may have been deluding myself. I hope not, but I'm prepared if so.
#!/usr/bin/env perl
chomp(@IPs = <DATA>);
@Sorted = map { substr($_, 4) }
sort map { pack('C*', split(/\./, $_) ) . $_ } @IPs;
print join("\n", @Sorted) . "\n";
__DATA__
12.345.111.3
12.345.111.26
61.8.47.111
12.345.111.24
203.134.35.27
12.345.111.5
12.345.111.4
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.