PHP natsort()Functions
Examples
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);
?>
$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);
?>
Run example »
Definition and Usage
The natsort() function sorts an array using a "natural order" algorithm. The key/value pairs retain their original key names.
In the natural ordering algorithm, the number 2 is less than the number 10. In the computer sorting algorithm, 10 is less than 2, because the first digit in "10" is less than 2.
Syntax
natsort(array)
| Parameter | Description |
|---|---|
| array | Required. Specifies the array to be sorted. |
Technical Details
| Return value: | Returns TRUE on success, or FALSE on failure. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | As of PHP 5.2.10, when zero-padding numeric strings (e.g., '00006'), the zero is ignored. |
Complete PHP Array Reference Other Extensions