PHP标准排序和自然排序
在自然排序算法中,数字 2 小于 数字 10。
在计算机标准排序算法中,10 小于 2,因为 “10” 中的第一个数字小于 2。
举例代码:
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "标准排序:";
print_r($temp_files);
echo "<br>";
natsort($temp_files);
echo "自然排序:";
print_r($temp_files);
输出结果得到如下:
标准排序:
Array
(
[0] => temp1.txt
[1] => temp10.txt
[2] => temp15.txt
[3] => temp2.txt
[4] => temp22.txt
)
自然排序:
Array
(
[0] => temp1.txt
[3] => temp2.txt
[1] => temp10.txt
[2] => temp15.txt
[4] => temp22.txt
)
函数说明
1.sort()函数对索引数组进行升序排序。
注意:本函数为数组中的单元赋予新的键名。原有的键名将被删除。
提示:请使用 rsort() 函数对索引数组进行降序排序。
2.natsort() 函数用"自然排序"算法对数组进行排序。
注意:键值保留它们原始的键名。
提示:自 PHP 5.2.10 起,当用 0 填充数字字符串时(例如 ‘00006’),将忽略 0。