What is PHP natsort() Function?
If you want to sort an array elements in a way that the human being would do (and regular computer string sorting algorithm doesn’t do it), then use PHP use natsort() function. This function performs a case-sensitive sorting.
So, what is the difference between human sorting and computer sorting? Well, consider two characters “i” and “Q”. According to ASCII table, the ASCII value of “i” is 105 and “Q”’s 81. So, computer considers “Q” lower than “i”. And, human considers “Q” higher than “i”. Human doesn’t consider data type when sorting inside an alphanumeric string.
Syntax:
natsort(array)
Parameters:
The Function has one parameter which is required-
array (Required): This is the array whose elements the function sorts
Return Values:
The function always returns TRUE.
Examples:
Example 1:
<pre>
<?php
$frameworks = ["1. PHP", "5. MySQL", "10. JavaScript", "15. WordPress", "20. Laravel"];
sort($frameworks);
echo "After sorting with sort() which sorts elements in ascending order is- <br />";
print_r($frameworks);
$frameworks = ["1. PHP", "5. MySQL", "10. JavaScript", "15. WordPress", "20. Laravel"];
natsort($frameworks);
echo "After sorting with natsort() which sorts elements in ascending order is- <br />";
print_r($frameworks);
?>
</pre>
Output:
After sorting with sort() which sorts elements in ascending order is-
Array
(
[0] => 1. PHP
[1] => 10. JavaScript
[2] => 15. WordPress
[3] => 20. Laravel
[4] => 5. MySQL
)
After sorting with natsort() which sorts elements in ascending order is-
Array
(
[0] => 1. PHP
[1] => 5. MySQL
[2] => 10. JavaScript
[3] => 15. WordPress
[4] => 20. Laravel
)
Explanation:
Within the string, the sort algorithm compares the first characters of the elements “20”and “5”. As 5 is greater than 2, so, it considers 5 is greater than 20.
Example 2:
<pre>
<?php
$frameworks = ["Symfony", "Laravel", "codeIgnitor", "CakePHP"];
natsort($frameworks);
echo "After sorting with natsort() which sorts elements in ascending order is- <br />";
print_r($frameworks);
?>
</pre>
Output:
After sorting with natsort() which sorts elements in ascending order is-
Array
(
[3] => CakePHP
[1] => Laravel
[0] => Symfony
[2] => codeIgnitor
)
Explanation:
In ASCII table small English letters have higher values than the capital English letters. So, the sort algorithm places “codeignitor” at last.
Notes on natsort() Function:
- The function retains the elements order those are equal. In the example below, see the index number of the two “Laravel”s. The first was earlier than the later one.
<pre> <?php $frameworks = ["Symfony", "Laravel", "CodeIgnitor", "CakePHP", "Laravel"]; echo "Original array-<br />"; print_r($frameworks); $frameworks = ["Symfony", "Laravel", "CodeIgnitor", "CakePHP", "Laravel"]; natsort($frameworks); echo "After sorting with netsort()-<br />"; print_r($frameworks); ?> </pre>
Output:
Original array-
Array
(
[0] => Symfony
[1] => Laravel
[2] => CodeIgnitor
[3] => CakePHP
[4] => Laravel
)
After sorting with netsort()-
Array
(
[3] => CakePHP
[2] => CodeIgnitor
[1] => Laravel
[4] => Laravel
[0] => Symfony
) - After running, the function set arrays internal pointer to the first item. The current() function returns the value of the element where the internal pointer is points. Check the example below-
<pre> <?php $frameworks = ["Symfony", "Laravel", "CodeIgnitor", "CakePHP"]; natsort($frameworks); echo "After sorting with netsort()- <br />"; print_r($frameworks); echo "The interbal pointer is pointing to- <br />"; echo current($frameworks); ?> </pre>
Output:
After sorting with netsort()-
Array
(
[3] => CakePHP
[2] => CodeIgnitor
[1] => Laravel
[0] => Symfony
)
The interbal pointer is pointing to-
CakePHP
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP natsort() Function
PHP has lots of built in array functions among them natsort() function is one of them. Use this function when you need a human like case sensitive sorting.