<?php
$files = array("file11.txt","file22.txt","file1.txt","file2.txt");
function mySort($arr,$nat=false) {
for($i=0;$i<count($arr);$i++) {
for($j=0;$j<count($arr)-$i-1;$j++) {
if($nat) {
//按照strnatcmp()自然排序函数
if(strnatcmp($arr[$j], $arr[$j+1])>0) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];//从小到大排
$arr[$j+1] = $tmp;
}
} else {
//按照strcmp()自然排序函数
if(strcmp($arr[$j], $arr[$j+1])>0) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];//从小到大排
$arr[$j+1] = $tmp;
}
}
}
}
return $arr;
}
//Array ( [0] => file1.txt [1] => file11.txt [2] => file2.txt [3] => file22.txt )
print_r(mySort($files,false));
echo "<br/>";
//2<11
//Array ( [0] => file1.txt [1] => file2.txt [2] => file11.txt [3] => file22.txt )
print_r(mySort($files,true));
?>
PHP 之 冒泡排序
最新推荐文章于 2023-12-05 16:36:38 发布