natsort
Sort arrays using the "natural sort" algorithm
natsort()
function uses the "natural sort" algorithm to sort the array. Key values retain their original key names.
In natural sorting algorithm, the number 2 is less than the number 10. In computer sorting algorithms, 10 is less than 2, because the first number in "10" is less than 2.
Sort the array:
<?php $temp_files = array ( "temp15.txt" , "temp10.txt" , "temp1.txt" , "temp22.txt" , "temp2.txt" ) ; sort ( $temp_files ) ; echo "Standard Sort:" ; print_r ( $temp_files ) ; echo "<br>" ; natsort ( $temp_files ) ; echo "Natural sorting:" ; print_r ( $temp_files ) ; ?>
Try it yourself
natsort ( array )
parameter | describe |
---|---|
array | Required. Specifies the array to be sorted. |
natsort()
function uses a natural order algorithm to sort elements in a given array.
natsort()
function implements "natural sorting", that is, the sorting method of numbers from 1 to 9, and the sorting method of letters from a to z, the shorter one takes priority. The index of the array remains associated with the unit value.
If successful, the function returns TRUE, otherwise FALSE.