<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Sorting 2D-arrays in PHP - anectodes and reflections</title>
	<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/</link>
	<description>Dev related notes, tutorials and anecdotes</description>
	<pubDate>Tue, 07 Oct 2008 09:09:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-380</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Wed, 09 Jan 2008 02:54:38 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-380</guid>
		<description>Leonard: That lib is one of my earlier creations, working but ugly "legacy" code that shows up now and then in my projects because I'm too lazy to prettify it. I don't want people to get the wrong ideas by showing it. But basically what it does in the above example is checking if the string is a date with a simple regular expression and you really have to know how it works because it might capture stuff that is not a date (quick, sloppy implementation that worked for the original project).

The date compare function just converts the dates to timestamps (with strtodate) and compares them as numbers instead.</description>
		<content:encoded><![CDATA[<p>Leonard: That lib is one of my earlier creations, working but ugly &#8220;legacy&#8221; code that shows up now and then in my projects because I&#8217;m too lazy to prettify it. I don&#8217;t want people to get the wrong ideas by showing it. But basically what it does in the above example is checking if the string is a date with a simple regular expression and you really have to know how it works because it might capture stuff that is not a date (quick, sloppy implementation that worked for the original project).</p>
<p>The date compare function just converts the dates to timestamps (with strtodate) and compares them as numbers instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1538</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Wed, 09 Jan 2008 02:54:38 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1538</guid>
		<description>Leonard: That lib is one of my earlier creations, working but ugly "legacy" code that shows up now and then in my projects because I&#39;m too lazy to prettify it. I don&#39;t want people to get the wrong ideas by showing it. But basically what it does in the above example is checking if the string is a date with a simple regular expression and you really have to know how it works because it might capture stuff that is not a date (quick, sloppy implementation that worked for the original project).&lt;br&gt;&lt;br&gt;The date compare function just converts the dates to timestamps (with strtodate) and compares them as numbers instead.</description>
		<content:encoded><![CDATA[<p>Leonard: That lib is one of my earlier creations, working but ugly &#8220;legacy&#8221; code that shows up now and then in my projects because I&#39;m too lazy to prettify it. I don&#39;t want people to get the wrong ideas by showing it. But basically what it does in the above example is checking if the string is a date with a simple regular expression and you really have to know how it works because it might capture stuff that is not a date (quick, sloppy implementation that worked for the original project).</p>
<p>The date compare function just converts the dates to timestamps (with strtodate) and compares them as numbers instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: René Leonhardt</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-376</link>
		<dc:creator>René Leonhardt</dc:creator>
		<pubDate>Tue, 08 Jan 2008 14:37:58 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-376</guid>
		<description>Where can the implementaiton of class StringManip be found?</description>
		<content:encoded><![CDATA[<p>Where can the implementaiton of class StringManip be found?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: René Leonhardt</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1149</link>
		<dc:creator>René Leonhardt</dc:creator>
		<pubDate>Tue, 08 Jan 2008 14:37:58 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1149</guid>
		<description>Where can the implementaiton of class StringManip be found?</description>
		<content:encoded><![CDATA[<p>Where can the implementaiton of class StringManip be found?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: developercast.com &#187; ProDevTips: Sorting 2D-arrays in PHP - anectodes and reflections</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-359</link>
		<dc:creator>developercast.com &#187; ProDevTips: Sorting 2D-arrays in PHP - anectodes and reflections</dc:creator>
		<pubDate>Mon, 07 Jan 2008 15:39:39 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-359</guid>
		<description>[...] the ProDevTips website today, there&#8217;s a new article looking at the sorting of arrays, specifically of the two- and three-dimensional sort.   One of the [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] the ProDevTips website today, there&#8217;s a new article looking at the sorting of arrays, specifically of the two- and three-dimensional sort.   One of the [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHPDeveloper.org</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-358</link>
		<dc:creator>PHPDeveloper.org</dc:creator>
		<pubDate>Mon, 07 Jan 2008 13:57:20 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-358</guid>
		<description>&lt;strong&gt;ProDevTips: Sorting 2D-arrays in PHP - anectodes and reflections...&lt;/strong&gt;


On the ProDevTips website today, there's a new article looking at ......</description>
		<content:encoded><![CDATA[<p><strong>ProDevTips: Sorting 2D-arrays in PHP - anectodes and reflections&#8230;</strong></p>
<p>On the ProDevTips website today, there&#8217;s a new article looking at &#8230;&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-357</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Mon, 07 Jan 2008 08:44:28 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-357</guid>
		<description>However the uasort function can come in handy to implement custom object sorting a la Java implements sortable.</description>
		<content:encoded><![CDATA[<p>However the uasort function can come in handy to implement custom object sorting a la Java implements sortable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1148</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Mon, 07 Jan 2008 08:44:28 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-1148</guid>
		<description>However the uasort function can come in handy to implement custom object sorting a la Java implements sortable.</description>
		<content:encoded><![CDATA[<p>However the uasort function can come in handy to implement custom object sorting a la Java implements sortable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-356</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Mon, 07 Jan 2008 08:41:47 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-356</guid>
		<description>I ended up discarding Andy's way of doing things. I couldn't figure out how to make it work with arbitrary keys and/or in a static function setting.

I ended up using array_multisort anyway:

&lt;pre&gt;&lt;code class="php"&gt;
static function sort2DAsc(&#038;$arr, $key){
	$sort_col = array(); 
	foreach ($arr as $sub) $sort_col[] = $sub[$key];
	if(is_numeric($sort_col[0]))
		array_multisort($sort_col, SORT_NUMERIC, $arr);
	else if(is_string($sort_col[0])){
		$sort_col = array_map('strtolower', $sort_col);
		array_multisort($sort_col, SORT_STRING, $arr);
	}
}

static function sort2DDesc(&#038;$arr, $key){
	$sort_col = array();
	foreach ($arr as $sub) $sort_col[] = $sub[$key];
	if(is_numeric($sort_col[0]))
		array_multisort($sort_col, SORT_NUMERIC, SORT_DESC, $arr);
	else if(is_string($sort_col[0])){
		$sort_col = array_map('strtolower', $sort_col);
		array_multisort($sort_col, SORT_STRING, SORT_DESC, $arr);
	}
}
&lt;/code&gt;&lt;/pre&gt;

Multisort is case sensitive and that is not the behavior I want so in addition to creating the whole sort column on the fly we also need to make it all lower case. It's not so beautiful anymore but it works and it is still shorter than the original monster(s).</description>
		<content:encoded><![CDATA[<p>I ended up discarding Andy&#8217;s way of doing things. I couldn&#8217;t figure out how to make it work with arbitrary keys and/or in a static function setting.</p>
<p>I ended up using array_multisort anyway:</p>
<pre><pre><code class="php">static function sort2DAsc(&amp;$arr, $key){
	$sort_col = array(); 
	foreach ($arr as $sub) $sort_col[] = $sub[$key];
	if(is_numeric($sort_col[0]))
		array_multisort($sort_col, SORT_NUMERIC, $arr);
	else if(is_string($sort_col[0])){
		$sort_col = array_map('strtolower', $sort_col);
		array_multisort($sort_col, SORT_STRING, $arr);
	}
}

static function sort2DDesc(&amp;$arr, $key){
	$sort_col = array();
	foreach ($arr as $sub) $sort_col[] = $sub[$key];
	if(is_numeric($sort_col[0]))
		array_multisort($sort_col, SORT_NUMERIC, SORT_DESC, $arr);
	else if(is_string($sort_col[0])){
		$sort_col = array_map('strtolower', $sort_col);
		array_multisort($sort_col, SORT_STRING, SORT_DESC, $arr);
	}
}</code></pre></pre>
<p>Multisort is case sensitive and that is not the behavior I want so in addition to creating the whole sort column on the fly we also need to make it all lower case. It&#8217;s not so beautiful anymore but it works and it is still shorter than the original monster(s).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik</title>
		<link>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-352</link>
		<dc:creator>Henrik</dc:creator>
		<pubDate>Mon, 07 Jan 2008 03:05:13 +0000</pubDate>
		<guid>http://www.prodevtips.com/2008/01/06/sorting-2d-arrays-in-php-anectodes-and-reflections/#comment-352</guid>
		<description>Even better, thanks Andy! I remember this way of doing things from Java, nice that it will work in PHP too. I didn't even know these u* functions existed!</description>
		<content:encoded><![CDATA[<p>Even better, thanks Andy! I remember this way of doing things from Java, nice that it will work in PHP too. I didn&#8217;t even know these u* functions existed!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
