通过简单的代码,比较php中sort,natsort和usort的不同。
成都创新互联公司成都网站建设专业公司,是成都网站制作公司,为会所设计提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站营销推广热线:028-86922220
<html> <head> <style type="text/css"> li{ border:2px solid black; padding: 0.5em; width: auto; margin: 0.5em; } p{ font-size: 20px; color: blue; } ol li{ border: 1px solid red; margin: 0px; } </style> </head> <body> <h4>My First Hello World in PHP</h4> <?php $dateFile = fopen("info.txt", "r"); while ($dateLine = fgets($dateFile, 4096)) { $dateList[] = $dateLine; } echo "<ul>"; echo "<li><p>Sorting the dates using sort() function:</p><ol>"; sort($dateList); foreach ($dateList as $date) { echo "<li>$date</li>"; } echo "</ol></li>"; echo "<li><p>Sorting the dates using natsort() function:</p><ol>"; natsort($dateList); foreach ($dateList as $date) { echo "<li>$date</li>"; } echo "</ol></li>"; 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 "<li><p>Sorting the dates using usort() with user defined comparing function:</p><ol>"; usort($dateList, 'opDateSort'); foreach ($dateList as $date) { echo "<li>$date</li>"; } echo "</ol></li>"; echo "</ul>"; ?> </body> </html>
其中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中的各种排序函数
本文路径:https://www.cdcxhl.com/article48/jcgdhp.html
成都网站建设公司_创新互联,为您提供关键词优化、企业建站、网站维护、全网营销推广、网站营销、ChatGPT
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联