in reply to Sorting ip addresses quickly
Sorting IP addresses is the canonical example of the Guttman-Rosler Transform (aka the packed default sort) given in A Fresh Look at Efficient Perl Sorting.
The paper is well-worth reading in detail, but here is the IP sorting code.
@out = map substr($_, 4) => sort map pack('C4' => /(\d+)\.(\d+)\.(\d+)\.(\d+)/) . $_ => @in;
Notice that by careful choice of a 'pack' function they can use the default sort behaviour, rather than writing a custom sort routine.
The benchmarks in the paper give this version as being about twice as fast as the Schwartzian Transform.
Update: URL replaced with one that works. Thanks to grinder for pointing it out.
--
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: RE: Sorting ip addresses quickly
by arturo (Vicar) on Oct 05, 2000 at 23:13 UTC |