热门IT资讯网

比较php中的各种排序函数

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,通过简单的代码,比较php中sort,natsort和usort的不同。li{border:2px solid black;padding: 0.5em;width: auto;margin: 0

通过简单的代码,比较php中sort,natsort和usort的不同。

My First Hello World in PHP

"; echo "
  • Sorting the dates using sort() function:

      "; sort($dateList); foreach ($dateList as $date) { echo "
    1. $date
    2. "; } echo "
  • "; echo "
  • Sorting the dates using natsort() function:

      "; natsort($dateList); foreach ($dateList as $date) { echo "
    1. $date
    2. "; } 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 "
    1. $date
    2. "; } echo "
  • "; echo "";?>

    其中info.txt的内容为

    4-1-196910-25-19879-11-19826-18-19284-27-19276-12-19237-16-19227-23-1921

    结果如图所示

    0