arsort ,asort,ksort,krsort等排序,无法数组同值稳定排序

 原因:PHP底层是用快排实现的。无法保证元素的顺序。

所以要使用natsort();

搬运一个w3c的例子

$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 )
发布了14 篇原创文章 · 获赞 3 · 访问量 4399

猜你喜欢

转载自blog.csdn.net/kina100/article/details/88761742