PHP natcasesort() Function

Definition and Usage

The natcasesort() function sorts the array using the "natural sorting" algorithm. Key values retain their original key names.

In natural sorting algorithms, the number 2 is less than the number 10. In computer sorting algorithms, 10 is less than 2, because the first digit of "10" is less than 2.

This function is case-insensitive.

If successful, the function returns TRUE, and if failed returns FALSE.

Syntax

natcasesort(array)
Parameter Description
array Required. Specifies the array to be sorted.

Example

<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural sorting:\n";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Case-insensitive natural sorting:\n";
print_r($temp_files);
?>

The output of the above code:

Natural sorting:
Array
(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
Case-insensitive natural order:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)