<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alan's blog &#187; sort</title>
	<atom:link href="http://www.alandix.com/blog/tag/sort/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alandix.com/blog</link>
	<description>just starting ...</description>
	<lastBuildDate>Wed, 08 Sep 2010 08:09:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		                        	<item>
		<title>Sorter</title>
		<link>http://www.alandix.com/blog/code/sorter/</link>
		<comments>http://www.alandix.com/blog/code/sorter/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 06:36:55 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?page_id=193</guid>
		<description><![CDATA[The Sorter class is made for sorting records where each record is an associative array.  For example, suppose $files is an array of the following form :
$files[] = array( 'name' =&#62; 'fred.txt', 'ext' =&#62; 'txt', 'size' =&#62; 1234 );$files[] = array( 'name' =&#62; 'jane.jpg', 'ext' =&#62; 'jpg', 'size' =&#62; 412 );$files[] = array( 'name' =&#62; [...]]]></description>
	      		<content:encoded><![CDATA[<p>The <code>Sorter</code> class is made for sorting records where each record is an associative array.  For example, suppose <code>$files</code> is an array of the following form :</p>
<pre style="padding-left: 30px;">$files[] = array( 'name' =&gt; 'fred.txt', 'ext' =&gt; 'txt', 'size' =&gt; 1234 );$files[] = array( 'name' =&gt; 'jane.jpg', 'ext' =&gt; 'jpg', 'size' =&gt; 412 );$files[] = array( 'name' =&gt; 'readme.txt', 'ext' =&gt; 'txt', 'size' =&gt; 592 );</pre>
<p>You can sort by extension type and size as follows:</p>
<pre style="padding-left: 30px;">$sorter = new Sorter("ext,-size:n");    // sort by extension (alphabetic) and descending size$sorter-&gt;sort($files);</pre>
<p>Basically it takes a comma separated list of field names (e.g. <code>"region,city,area"</code>), each of which can optionally be preceded by a &#8220;+&#8221; (default) or &#8220;-&#8221; to say whether it is an ascending or descending sort and suffixed with a type letter (e.g &#8220;:n&#8221;) to say whether the sort is alphabetic (&#8220;s&#8221; and default), numeric (&#8220;n&#8221;) or by date (&#8220;d&#8221;).  The date has to be in a format PHP recognises, but simple timestamps are numeric.</p>
<p>I think this should also work for arrays of numerically indexed arrays, so that <code>"3,-2:d"</code> sorts by column 3 and then column 2 as a date descending, but I&#8217;ve not tested this.</p>
<h3>the code</h3>
<pre class="brush: php;">
class Sorter {
  var $keys;
  function Sorter($keyspec) {
      $this-&gt;keys = $this-&gt;parseKeys($keyspec);
   }
   function sort( &amp;amp;$arr ) {
      return usort( $arr, array($this,'compare') );
   }
   function parseKeys($keyspec) {
      $keyspecs = explode(&quot;,&quot;,$keyspec);
      $keys = array();
      foreach ( $keyspecs as $onespec ) {
         if ( $onespec{0} == '-' ) {
            $onespec = substr($onespec,1);
            $dir = '-';
         } else if ( $onespec{0} == '+' ) {
            $onespec = substr($onespec,1);
            $dir = '+';
         } else {
            $dir = '+';
         }
         $parts = explode(':',$onespec);
         if ( $parts[0] ) {
            switch ( $parts[1] ) {
               case 'i': case 'n':
                  $type = 'n';  break;  // number
               case 'd':
                  $type = 'd';  break;  // date
               case 's': default:
                  $type = 's';  break;  // string
            }
            $keys[] = array( $parts[0], $dir, $type );
         }
      }
      return $keys;
   }
   function compare( $obj1, $obj2 ) {
      foreach( $this-&gt;keys as $key ) {
         list( $name, $dir, $type ) = $key;
         $fld1 = $obj1[$name];  $fld2 = $obj2[$name];
         switch ( $type ) {
            case 'n':
               $cmp = $fld1 - $fld2;
               break;
            case 'd':
               $time1 = strtotime($fld1);
               $time2 = strtotime($fld2);
               if ( $time1 === false ||  $time1 === -1 ||
                    $time2 === false ||  $time2 === -1 ) {
                        // false PHP 5.1, -1 older PHP
                  $cmp = 0;    // badly formed dates
               } else {
                  $cmp = $time1 - $time2;
               }
            case 's': default:
               $cmp = strcmp($fld1,$fld2);
               break;
         }
         if ( $cmp == 0 ) continue;
         if ( $dir == '-' ) $cmp = - $cmp;
         return $cmp;
      }
      return 0;
   }
}
</pre>
]]></content:encoded>
	  			<wfw:commentRss>http://www.alandix.com/blog/code/sorter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
            	                        	<item>
		<title>a simple PHP record sorter class</title>
		<link>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/</link>
		<comments>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 11:51:31 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=179</guid>
		<description><![CDATA[Not for the first time I needed to sort arrays of arrays in PHP (structures like tiny DB tables).  I have previously written little wrapper functions round usort, but decided this time to make a small class. It is a simple, but generic utility, so popping it up in case useful to anyone.
The rest of [...]]]></description>
	      		<content:encoded><![CDATA[<p>Not for the first time I needed to sort arrays of arrays in PHP (structures like tiny DB tables).  I have previously written little wrapper functions round <a href="http://uk.php.net/manual/en/function.usort.php" target="_blank">usort</a>, but decided this time to make a small class. It is a simple, but generic utility, so popping it up in case useful to anyone.</p>
<p>The rest of this post has moved to a permanent page at:</p>
<p style="padding-left: 30px;"><a href="http://www.alandix.com/blog/code/sorter/" target="_self">http://www.alandix.com/blog/code/sorter/</a></p>
]]></content:encoded>
	  			<wfw:commentRss>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
            	</channel>
</rss>
