duan89197 2011-10-25 18:47
浏览 45
已采纳

如何按日期排序文件列表

Server returns this:

<div id="urls">

   04.10.2011-some-article.html   <br />
   10.09.2011-other-article.html   <br />
   14.07.2010-some-text.html   <br />
   18.09.2011-article.html   <br />
   25.10.2011-text.html   <br />

</div>

But I'd like to sort this articles by the actual creation date. Is there any way to do it with jQuery (or immediately with php?)

please give me suggestions I appreciate your interest and help!!

  • 写回答

2条回答 默认 最新

  • dongqing5925 2011-10-25 19:16
    关注

    First-off, this is pretty bad data to be getting from the server. Looks like it might change without notice, etc. If it's from an API, you should seek a better response format such as JSON.

    Nevertheless, here is some PHP that would sort your articles by date.

    <?php
    
    $serverData = <<<EOF
       04.10.2011-some-article.html   <br />
       10.09.2011-other-article.html   <br />
       14.07.2010-some-text.html   <br />
       18.09.2011-article.html   <br />
       25.10.2011-text.html   <br />
    EOF;
    
    $articles = explode("
    ", $serverData);
    usort($articles, function($a, $b) {
        $times = array();
        foreach (array($a, $b) as $article) {
            if ($article) {
                list($date, $title) = explode('-', trim($article), 2);
                list($day, $month, $year) = explode('.', trim($date));
                $times[] = strtotime("$year-$month-$day");
            } else {
                $times[] = 0;
            }
        }
        return $times[1] - $times[0];
    });
    
    print_r($articles);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系