Let’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 “Mission Impossible” or “Pirates of the Carribean”. We know that they are often followed by the consecutive number of the episode.
Mission: Impossible 1 Mission: Impossible 2 Mission: Impossible 3 ...
Since we have no more than three or four episodes we can easily sort the array if it’s not sorted initially.
$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);
However in some cases we can have more than 10 episodes. Then we can meet a problem while sorting the array above.
$a = array('Episode 1', 'Episode 2', 'Episode 11', 'Episode 112'); sort($a); // Episode 1 // Episode 11 // Episode 112 // Episode 2 print_r($a);
Now because this is by default an alphabetical sort order we get an array that isn’t sorted to our human undestanding.
The question is how to overcome this problem?
Continue reading PHP and MySQL Natural Sort