<?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>Pirates of the Carribean &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/pirates-of-the-carribean/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>PHP and MySQL Natural Sort</title>
		<link>/2012/06/07/php-and-mysql-natural-sort/</link>
		<comments>/2012/06/07/php-and-mysql-natural-sort/#comments</comments>
		<pubDate>Thu, 07 Jun 2012 11:43:05 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[alphabetical order]]></category>
		<category><![CDATA[Entertainment/Culture]]></category>
		<category><![CDATA[Mission: Impossible]]></category>
		<category><![CDATA[Mission: Impossible 2]]></category>
		<category><![CDATA[Mission: Impossible 3]]></category>
		<category><![CDATA[MySQL AB]]></category>
		<category><![CDATA[natural sort order]]></category>
		<category><![CDATA[Order by]]></category>
		<category><![CDATA[Pirates of the Carribean]]></category>
		<category><![CDATA[Sorting]]></category>
		<category><![CDATA[Sorting algorithms]]></category>

		<guid isPermaLink="false">/?p=3176</guid>
		<description><![CDATA[Use Case Let&#8217;s say we have an array of data represented by some text followed by a number. Just like the movies from a movie series like &#8220;Mission Impossible&#8221; or &#8220;Pirates of the Carribean&#8221;. We know that they are often followed by the consecutive number of the episode. Mission: Impossible 1 Mission: Impossible 2 Mission: &#8230; <a href="/2012/06/07/php-and-mysql-natural-sort/" class="more-link">Continue reading <span class="screen-reader-text">PHP and MySQL Natural Sort</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2010/07/09/friday-algorithms-javascript-bubble-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Bubble Sort">Friday Algorithms: JavaScript Bubble Sort </a></li>
<li><a href="/2012/02/20/computer-algorithms-bubble-sort/" rel="bookmark" title="Computer Algorithms: Bubble Sort">Computer Algorithms: Bubble Sort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Use Case</h2>
<p>Let&#8217;s say we have an array of data represented by some text followed by a number. Just like the movies from a movie series like &#8220;Mission Impossible&#8221; or &#8220;Pirates of the Carribean&#8221;. We know that they are often followed by the consecutive number of the episode. </p>
<pre lang="PHP">
Mission: Impossible 1
Mission: Impossible 2
Mission: Impossible 3
...
</pre>
<p>Since we have no more than three or four episodes we can easily sort the array if it&#8217;s not sorted initially.</p>
<pre lang="PHP">
$a = array('Mission: Impossible 2', 'Mission: Impossible 3', 'Mission: Impossible 1');

sort($a);

// Mission: Impossible 1
// Mission: Impossible 2
// Mission: Impossible 3
print_r($a);
</pre>
<p>However in some cases we can have more than 10 episodes. Then we can meet a problem while sorting the array above.</p>
<pre lang="PHP">
$a = array('Episode 1', 'Episode 2', 'Episode 11', 'Episode 112');

sort($a);

// Episode 1
// Episode 11
// Episode 112
// Episode 2
print_r($a);
</pre>
<p>Now because this is by default an alphabetical sort order we get an array that isn&#8217;t sorted to our human undestanding.</p>
<figure id="attachment_3189" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/Natural-Sort.png"><img src="/wp-content/uploads/2012/06/Natural-Sort.png" alt="Natural Sort" title="Natural Sort" width="620" height="399" class="size-full wp-image-3189" srcset="/wp-content/uploads/2012/06/Natural-Sort.png 620w, /wp-content/uploads/2012/06/Natural-Sort-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Alphabetical vs. Natural sort order</figcaption></figure>
<p>The question is how to overcome this problem?<br />
<span id="more-3176"></span></p>
<h2>PHP</h2>
<p>First the thing we actually need is called &#8220;natural sort&#8221;, so PHP (with its full of handful functions library) takes care for us with <a href="http://www.php.net/manual/en/function.natsort.php" title="PHP: natsort" target="_blank">natsort</a>.</p>
<pre lang="PHP">
$a = array('Episode 1', 'Episode 2', 'Episode 11', 'Episode 112');

natsort($a);

// Episode 1
// Episode 2
// Episode 11
// Episode 112
print_r($a);
</pre>
<p>Now the array is sorted accordingly.</p>
<h2>MySQL</h2>
<p>MySQL in the other hand appears to be more hostile to natural sorting. We can just have ORDER BY with some keyword in order to sort a column using natural sort. </p>
<p>Given the table data:</p>
<pre lang="PHP">
my_table
-----------------------------------------
|	id	|	name		|
-----------------------------------------
|	1	|	Episode 2	|
|	2	|	Episode 1	|
|	3	|	Episode 112	|
|	4	|	Episode 11	|
-----------------------------------------
</pre>
<pre lang="SQL">
SELECT * FROM my_table ORDER BY name;
</pre>
<p>The query above will return the table in an alphabetical order.</p>
<pre lang="PHP">
my_table
-----------------------------------------
|	id	|	name		|
-----------------------------------------
|	2	|	Episode 1	|
|	4	|	Episode 11	|
|	3	|	Episode 112	|
|	1	|	Episode 2	|
-----------------------------------------
</pre>
<p>However there are some &#8220;hacks&#8221;. Here&#8217;s one of them.</p>
<pre lang="SQL">
SELECT * FROM my_table ORDER BY LENGTH(name), name;
</pre>
<p>Now the column is sorted correctly.</p>
<pre lang="PHP">
my_table
-----------------------------------------
|	id	|	name		|
-----------------------------------------
|	2	|	Episode 1	|
|	1	|	Episode 2	|
|	4	|	Episode 11	|
|	3	|	Episode 112	|
-----------------------------------------
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2010/07/09/friday-algorithms-javascript-bubble-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Bubble Sort">Friday Algorithms: JavaScript Bubble Sort </a></li>
<li><a href="/2012/02/20/computer-algorithms-bubble-sort/" rel="bookmark" title="Computer Algorithms: Bubble Sort">Computer Algorithms: Bubble Sort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/06/07/php-and-mysql-natural-sort/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
