<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Sorting PHP arrays with substrings</title>
	<atom:link href="http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Fri, 27 Jan 2012 16:39:42 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: eleg</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/comment-page-1/#comment-1638</link>
		<dc:creator>eleg</dc:creator>
		<pubDate>Mon, 18 Jan 2010 22:31:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=2141#comment-1638</guid>
		<description>please strip the line:

&lt;code&gt;
 $b.($b?&quot;:&quot;:&quot;_&quot;)
.str_pad($a,10,&quot;_&quot;,STR_PAD_LEFT);&#039;);
&lt;/code&gt;

&gt;:-[</description>
		<content:encoded><![CDATA[<p>please strip the line:</p>
<p><code><br />
 $b.($b?":":"_")<br />
.str_pad($a,10,"_",STR_PAD_LEFT);');<br />
</code></p>
<p>&gt;:-[</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eleg</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/comment-page-1/#comment-1637</link>
		<dc:creator>eleg</dc:creator>
		<pubDate>Mon, 18 Jan 2010 22:28:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=2141#comment-1637</guid>
		<description>oops (^Z)

corrected to:
&lt;code&gt;
$max = array_reduce($valeurs,create_function(&#039;$a,$b&#039;, &#039;return (strlen($a)&lt;strlen($b)?$b:$a);&#039;));
$max=strlen($max);
$f1 = create_function(&#039;$v&#039;, &#039;global $max; list($a,$b)=explode(&quot;:&quot;,$v);return $b.($b?&quot;:&quot;:&quot;_&quot;).str_pad($a,$max,&quot;_&quot;,STR_PAD_LEFT);&#039;);
$b.($b?&quot;:&quot;:&quot;_&quot;).str_pad($a,10,&quot;_&quot;,STR_PAD_LEFT);&#039;);
$f2 = create_function(&#039;$v&#039;, &#039;list($a,$b)=explode(&quot;:&quot;,$v);return ltrim($b,&quot;_&quot;).($b?&quot;:&quot;.$a:ltrim($a,&quot;_&quot;));&#039;);

$valeurs = array_map($f1,$valeurs);

sort($valeurs);

$valeurs = array_map($f2,$valeurs);
&lt;/code&gt;
-&gt; 4.84191417694 (still better, probably not optimal)</description>
		<content:encoded><![CDATA[<p>oops (^Z)</p>
<p>corrected to:<br />
<code><br />
$max = array_reduce($valeurs,create_function('$a,$b', 'return (strlen($a)&lt;strlen($b)?$b:$a);'));<br />
$max=strlen($max);<br />
$f1 = create_function('$v', 'global $max; list($a,$b)=explode(":",$v);return $b.($b?":":"_").str_pad($a,$max,"_",STR_PAD_LEFT);');<br />
$b.($b?":":"_").str_pad($a,10,"_",STR_PAD_LEFT);');<br />
$f2 = create_function('$v', 'list($a,$b)=explode(":",$v);return ltrim($b,"_").($b?":".$a:ltrim($a,"_"));');</p>
<p>$valeurs = array_map($f1,$valeurs);</p>
<p>sort($valeurs);</p>
<p>$valeurs = array_map($f2,$valeurs);<br />
</code><br />
-&gt; 4.84191417694 (still better, probably not optimal)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eleg</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/comment-page-1/#comment-1636</link>
		<dc:creator>eleg</dc:creator>
		<pubDate>Mon, 18 Jan 2010 21:57:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=2141#comment-1636</guid>
		<description>

&lt;code&gt;
$f1 = create_function(&#039;$v&#039;, &#039;list($a,$b)=explode(&quot;:&quot;,$v);return $b.($b?&quot;:&quot;:&quot;_&quot;).$a;&#039;);
$f2 = create_function(&#039;$v&#039;, &#039;list($a,$b)=explode(&quot;:&quot;,$v);return $b.($b?&quot;:&quot;.$a:ltrim($a,&quot;_&quot;));&#039;);

$valeurs = array_map($f,$valeurs);

sort($valeurs);

$valeurs = array_map($f,$valeurs);
&lt;/code&gt;

array of 3000 items :
 yours = 1.96446013451
 this    = 0.0130748748779

~30000 items :
26.4119880199
0.182013988495

seems coherent results.

I don&#039;t even know why I have imagined such a solution! or why it looks like more efficient!</description>
		<content:encoded><![CDATA[<p><code><br />
$f1 = create_function('$v', 'list($a,$b)=explode(":",$v);return $b.($b?":":"_").$a;');<br />
$f2 = create_function('$v', 'list($a,$b)=explode(":",$v);return $b.($b?":".$a:ltrim($a,"_"));');</p>
<p>$valeurs = array_map($f,$valeurs);</p>
<p>sort($valeurs);</p>
<p>$valeurs = array_map($f,$valeurs);<br />
</code></p>
<p>array of 3000 items :<br />
 yours = 1.96446013451<br />
 this    = 0.0130748748779</p>
<p>~30000 items :<br />
26.4119880199<br />
0.182013988495</p>
<p>seems coherent results.</p>
<p>I don&#8217;t even know why I have imagined such a solution! or why it looks like more efficient!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeichhor</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/comment-page-1/#comment-1562</link>
		<dc:creator>jeichhor</dc:creator>
		<pubDate>Wed, 16 Dec 2009 21:51:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.codediesel.com/?p=2141#comment-1562</guid>
		<description>if you have plenty of RAM you could give this a try:
&lt;code&gt;
&lt;?php

$values = array(&quot;66345:CDF&quot;, &quot;61179:HGT&quot;, &quot;64146:ABA&quot;,
                &quot;68768:BNG&quot;, &quot;68015:ZCZ&quot;, &quot;80231:LPO&quot;,
                &quot;64146:QWP&quot;, &quot;68736:HHB&quot;, &quot;86801:MNV&quot;,
                &quot;80178:OIU&quot;, &quot;80178:ASE&quot;, &quot;88178:BRT&quot;,
                &quot;801782OIU&quot;, &quot;801378ASE&quot;, &quot;881578BRT&quot;);

//get more values for longer experience
for ($z1=0;$z1sizeof($values)
	,&quot;time to beat&quot;=&gt;$t1-$t0
	,&quot;time took&quot;=&gt;$t2-$t1
) );
 
?&gt;

22.43:je@8@booster:~
$ /usr/local/php5/bin/php /var/www/test.php
array(3) {
  [&quot;number of items to sort:&quot;]=&gt;
  int(15360)
  [&quot;time to beat&quot;]=&gt;
  float(6.2945919036865)
  [&quot;time took&quot;]=&gt;
  float(2.0644869804382)
}
22.47:je@8@booster:~
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>if you have plenty of RAM you could give this a try:<br />
<code><br />
&lt;?php</p>
<p>$values = array("66345:CDF", "61179:HGT", "64146:ABA",<br />
                "68768:BNG", "68015:ZCZ", "80231:LPO",<br />
                "64146:QWP", "68736:HHB", "86801:MNV",<br />
                "80178:OIU", "80178:ASE", "88178:BRT",<br />
                "801782OIU", "801378ASE", "881578BRT");</p>
<p>//get more values for longer experience<br />
for ($z1=0;$z1sizeof($values)<br />
	,"time to beat"=&gt;$t1-$t0<br />
	,"time took"=&gt;$t2-$t1<br />
) );</p>
<p>?&gt;</p>
<p>22.43:je@8@booster:~<br />
$ /usr/local/php5/bin/php /var/www/test.php<br />
array(3) {<br />
  ["number of items to sort:"]=&gt;<br />
  int(15360)<br />
  ["time to beat"]=&gt;<br />
  float(6.2945919036865)<br />
  ["time took"]=&gt;<br />
  float(2.0644869804382)<br />
}<br />
22.47:je@8@booster:~<br />
</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>

