<?xml version="1.0" encoding="windows-1252"?>
<node id="25958" title="RE: Sorting a list of IP addresses (aka Why I hate Big O)" created="2000-08-03 12:25:45" updated="2005-07-19 14:08:39">
<type id="11">
note</type>
<author id="8307">
DrManhattan</author>
<data>
<field name="doctext">
&lt;p&gt;Read further down in the &lt;a href="http://www.hpl.hp.com/personal/Larry_Rosler/sort/sorting.html"&gt;Guttman/Rosler&lt;/a&gt;
paper to where they discuss the "packed-default" sort:&lt;/p&gt;
&lt;code&gt;
@out =
  map  substr($_, 4) =&gt;
  sort
  map  pack('C4' =&gt;
    /(\d+)\.(\d+)\.(\d+)\.(\d+)/)
      . $_ =&gt; @in;
&lt;/code&gt;
&lt;p&gt;The idea is to pack the entire list once before sorting
it then get each address back out with a substr afterwards.
Using this technique, you only make N packs and N substrs,
instead of N*log(N) packs.  It also has the advantage of
using sort without calling a subroutine for each comparison
which is a huge speedup.&lt;/p&gt;
&lt;p&gt;-Matt&lt;/p&gt;</field>
<field name="root_node">
25833</field>
<field name="parent_node">
25833</field>
</data>
</node>
