$a = "111.101.202.1.0";
$b = "111.101.202.1.1";
$va = explode('.', $a);
$vb = explode('.', $b);
//比较函数
function comparefun($va,$vb){
$a = array_shift($va);
$b = array_shift($vb);
if($a>$b){
echo '前者版本高';
return 0;
}elseif($a
echo '后者版本高';
return 0;
}else{
return comparefun($va,$vb);
}
}
comparefun($va,$vb);
然后怎么排序呢?似乎面试的最后都是排序题
做一个简单的冒泡排序:
$versionArr=array(
"111.101.202.1.0",
"111.101.202.1.1",
"112.101.202.1.1",
"111.103.202.1.1",
"111.103.201.0.1"
);
//比较函数
function comparefun($va,$vb){
$a = array_shift($va);
$b = array_shift($vb);
if($a>$b){
return 1;
}elseif($a
return 0;
}else{
return comparefun($va,$vb);
}
}
array_walk($versionArr, function(&$value){
$value = explode('.', $value);
});
for($i=0;$i
for($j=0;$j
if(comparefun($versionArr[$j],$versionArr[$j+1])){
$tmp = $versionArr[$j];
$versionArr[$j] = $versionArr[$j+1];
$versionArr[$j+1] = $tmp;
}
}
}
array_walk($versionArr, function(&$value){
$value = implode('.', $value);
});
var_dump($versionArr);