<?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>Nate&#039;s blog &#187; version</title>
	<atom:link href="http://www.natenewz.com/tag/version/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.natenewz.com</link>
	<description>My various projects</description>
	<lastBuildDate>Wed, 27 Oct 2010 00:07:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sorting Version Numbers in PHP</title>
		<link>http://www.natenewz.com/2010/06/29/sorting-version-numbers-in-php/</link>
		<comments>http://www.natenewz.com/2010/06/29/sorting-version-numbers-in-php/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 17:51:50 +0000</pubDate>
		<dc:creator>nate</dc:creator>
				<category><![CDATA[None]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[version]]></category>

		<guid isPermaLink="false">http://www.natenewz.com/?p=371</guid>
		<description><![CDATA[UPDATE:
I found a php function which takes much of the hardest part of this problem and makes it easier. The function is version_compare().
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
What&#8217;s the problem? Say you have a version number such as 1.2.3 stored in a MySql database as a varchar, and you want to sort this version number. Without any decimals, here is [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATE:</p>
<p>I found a php function which takes much of the hardest part of this problem and makes it easier. The function is <a title="version_compare" href="http://php.net/manual/en/function.version-compare.php" target="_blank">version_compare</a>().</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>What&#8217;s the problem? Say you have a version number such as 1.2.3 stored in a MySql database as a varchar, and you want to sort this version number. Without any decimals, here is the sorting descending:<br />
+&#8212;&#8212;&#8212;+<br />
| version |<br />
+&#8212;&#8212;&#8212;+<br />
| 9960    |<br />
| 9952    |<br />
| 9765    |<br />
| 9764    |<br />
| 10011   |<br />
+&#8212;&#8212;&#8212;+<br />
5 rows in set (0.00 sec)<br />
It thinks 9960 &gt; 10011<br />
If you don&#8217;t have a decimal point this can be easily remedied by doing:<br />
ORDER BY CAST(version AS UNSIGNED) DESC<br />
But as soon as you put a decimal into the version, it ignores everything to the right of the decimal point.<br />
My solution is a PHP sorting algorithm.<br />
$versions is an indexed array that has this format, which could easily come from a database<br />
array<br />
0 =&gt;<br />
array<br />
&#8216;id&#8217; =&gt; int 88<br />
&#8216;version&#8217; =&gt;<br />
array<br />
0 =&gt; int 0<br />
1 =&gt; int 9<br />
2 =&gt; int 10011<br />
1 =&gt;<br />
array<br />
&#8216;id&#8217; =&gt; int 87<br />
&#8216;version&#8217; =&gt;<br />
array<br />
0 =&gt; int 0<br />
1 =&gt; int 9<br />
2 =&gt; int 9960</p>
<pre id="PHP" class="prettyprint">// convert all values to ints
array_walk_recursive($versions, function(&amp;$val, $i)
{
    if (is_string($val))
        $val = (int)$val;
});
// make to arrays the same length by padding them with 0s
function normalize($a, $b)
{
    $al = count($a);
    $bl = count($b);
    if ($al < $bl)
    {
        // add 0s to $a until they are the same size
        while (count($a) < $bl)
            $a[] = 0;
    }
    elseif ($al > $bl)
    {
        // add 0s to $b until they are the same size
        while ($al > count($b))
            $b[] = 0;
    }
    return array($a, $b);
}
//compare to integers and decide which one is bigger
// return 0 if they are equal
function check($a, $b)
{
    if ($a < $b)
        return 1;
    elseif ($a > $b)
        return -1;
    else
        return 0;
}
// sort the array
usort($versions, function($a, $b)
{
    // make sure the major_version arrays have the same length, pad with 0s to correct
    list($a['version'], $b['version']) = normalize($a['version'], $b['version']);
    for ($i=0; $i < count($a['version']); $i++)
        $check = check($a['version'][$i], $b['version'][$i]);
    return $check;
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.natenewz.com/2010/06/29/sorting-version-numbers-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

