PHP natcasesort() Function

What is PHP natcasesort() Function?

If you want to sort an array elements in a way that the human being would do (and regular computer string sorting algorithm does do it), then use PHP usenatcasesort() function. This function performs a case-insentive 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”. On human considers “Q” higher “i”. Human doesn’t consider data type when sorting inside a alphanumeric string.

Syntax:

natcasesort(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 elements with sort() which sorts in ascendical order- <br />";
print_r($frameworks);
$frameworks = ["1. PHP", "5. MySQL", "10. JavaScript", "15. WordPress", "20. Laravel"];
natcasesort($frameworks);
echo "After sorting with netcasesort()-<br />";
print_r($frameworks);
?>
</pre>

Output:

After sorting with sort() which sorts elements in ascending order-
Array
(
    [0] => 1. PHP
    [1] => 10. JavaScript
    [2] => 15. WordPress
    [3] => 20. Laravel
    [4] => 5. MySQL
)
After sorting with netcasesort()-
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"];
sort($frameworks);
echo "After sorting elements with sort() which sorts in ascendical order- <br />";
print_r($frameworks);
$frameworks = ["Symfony", "Laravel", "codeIgnitor", "CakePHP"]; // case insensitive search.
natcasesort($frameworks);
echo "After sorting with netcasesort()-<br />";
print_r($frameworks);
?>
</pre>

Output:

After sorting with sort() which sorts elements in ascending order-
Array
(
    [0] => CakePHP
    [1] => Laravel
    [2] => Symfony
    [3] => codeIgnitor
)
After sorting with netcasesort()-
Array
(
    [3] => CakePHP
    [2] => codeIgnitor
    [1] => Laravel
    [0] => Symfony
)

Explanation:

In ASCII table small English letters have higher values than the capital English letters. So, the sort algorithm places “codeignitor” at last.

Notes onnatcasesort() 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"];
    natcasesort($frameworks);
    echo "After sorting with netcasesort()-<br />";
    print_r($frameworks);
    ?>
    </pre>
    

    Output:

    Original array-
    Array
    (
    [0] => Symfony
    [1] => Laravel
    [2] => CodeIgnitor
    [3] => CakePHP
    [4] => Laravel
    )
    After sorting with netcasesort-
    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"];
    natcasesort($frameworks);
    echo "After sorting with netcasesort()- <br />";
    print_r($frameworks);
    echo "The interbal pointer is pointing to- <br />";
    echo current($frameworks);
    ?>
    </pre>
    

    Output:

    After sorting with netcasesort()-
    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 natcasesort() Function

PHP has lots of built in array functions among them natcasesort() function is one of them. Use this function when you need a human like sorting.

Reference:

https://www.php.net/manual/en/function.natcasesort.php