通过简单的代码,比较php中sort,natsort和usort的不同。
成都创新互联公司成都网站建设专业公司,是成都网站制作公司,为会所设计提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站营销推广热线:028-86922220
My First Hello World in PHP
";
echo "
Sorting the dates using sort() function:
";
sort($dateList);
foreach ($dateList as $date)
{
echo "- $date
";
}
echo "
";
echo "
Sorting the dates using natsort() function:
";
natsort($dateList);
foreach ($dateList as $date)
{
echo "- $date
";
}
echo "
";
function opDateSort($a, $b)
{
if ($a == $b) return 0;
list($aMonth, $aDay, $aYear) = explode("-", $a);
list($bMonth, $bDay, $bYear) = explode("-", $b);
$aMonth = str_pad($aMonth, 2, "0", STR_PAD_LEFT);
$bMonth = str_pad($bMonth, 2, "0", STR_PAD_LEFT);
$aDay = str_pad($aDay, 2, "0", STR_PAD_LEFT);
$bDay = str_pad($bDay, 2, "0", STR_PAD_LEFT);
$aDate = $aYear.$aMonth.$aDay;
$bDate = $bYear.$bMonth.$bDay;
return $aDate>$bDate?1:-1;
}
echo "
Sorting the dates using usort() with user defined comparing function:
";
usort($dateList, 'opDateSort');
foreach ($dateList as $date)
{
echo "- $date
";
}
echo "
";
echo "";
?>
其中info.txt的内容为
4-1-1969
10-25-1987
9-11-1982
6-18-1928
4-27-1927
6-12-1923
7-16-1922
7-23-1921
结果如图所示
新闻标题:比较php中的各种排序函数
网址分享:
http://cxhlcq.com/article/jcgdhp.html