PHP natsort() Function
Example
Sort an array:
<?php
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "<br>";
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
?>
Try it Yourself »
Definition and Usage
The natsort() function sorts an array by using a "natural order" algorithm. The values keep their original keys.
In a natural algorithm, the number 2 is less than the number 10. In computer sorting, 10 is less than 2, because the first number in "10" is less than 2.
Syntax
natsort(array)
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifies the array to sort |
Technical Details
Return Value: | Returns TRUE on success, or FALSE on failure. |
---|---|
PHP Version: | 4+ |
PHP Changelog: | PHP 5.2.1: Zero padded numeric strings (e.g., '00006') now ignore the 0 padding |
❮ PHP Array Reference