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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记