doufei9805 2017-10-23 22:44
浏览 163

自然地排序scandir的结果?

I'm trying to sort an array containing the results of a scandir function. I've tried using the natsort() php function, but it doesn't appear to be working as I need it to for my directories.

The contents of the scanned directory assume a HH(Z)DDMonYYYY naming convention. After using the natsort function on the array, the result is this:

$dates = ["21Z23Oct2017", "20Z23Oct2017", "19Z23Oct2017", 
          "19Z18Oct2017", "19Z17Oct2017", "19Z16Oct2017",
          "18Z23Oct2017", "18Z18Oct2017", "17Z23Oct2017", ...]

As you can see, the function is evaluating the first two digits and using them to sort, but it ignores the days (23, 18, 17, 16) in each name.

I would like for the resulting array to look like this:

$dates = ["21Z23Oct2017", "20Z23Oct2017", "19Z23Oct2017", 
          "18Z23Oct2017", "17Z23Oct2017", ...,
          "19Z18Oct2017", "18Z18Oct2017", "19Z17Oct2017", "19Z16Oct2017"]

Since the directories are created sequentially, I realize that I could sort by directory creation or modification time and be just fine 99% of the time. However, on rare occasions the directories modification times will not be in perfect order, and I'd like to avoid problems when that is the case.

Is there a way to accomplish my goal in php without having to use modification or creation times?

Thanks to all in advance!

EDIT: for context, I'm using a Python script to write to and perform a simple operation on each of these directories. Python includes a package called "natsorted" for those unaware, which sorts the directories in the example array above without any trouble. Just wondering if there was a simple php solution as well before I start adding complexity.

  • 写回答

1条回答 默认 最新

  • dongxian8048 2017-10-23 23:04
    关注

    All that natsort() does is try to address the sorting of strings with arbitrary-length digit sequences, it does not magically interpret bizarre date formats. Even the PHP functions that do try to figure out dates would not be able to figure this one out as they actually just use a predefined list of common formats and even then are problematic.

    IMHO you should always use something like DateTime::createFromFormat() and an explicit format string.

    <?php
    
    $dates = ["21Z23Oct2017", "20Z23Oct2017", "19Z23Oct2017", 
              "19Z18Oct2017", "19Z17Oct2017", "19Z16Oct2017",
              "18Z23Oct2017", "18Z18Oct2017", "17Z23Oct2017"];
    
    usort(
        $dates,
        function($a,$b){
            return DateTime::createFromFormat("H\ZdMY",$a) <=> DateTime::createFromFormat("H\ZdMY",$b);
        }
    );
    
    echo json_encode($dates, JSON_PRETTY_PRINT);
    

    Output:

    [
        "19Z16Oct2017",
        "19Z17Oct2017",
        "18Z18Oct2017",
        "19Z18Oct2017",
        "17Z23Oct2017",
        "18Z23Oct2017",
        "19Z23Oct2017",
        "20Z23Oct2017",
        "21Z23Oct2017"
    ]
    

    This will work adequately for small sets of dates and/or when called infrequently. However if you're sorting a large number of dates, or sorting them frequently, you're going to want to pre-create the DateTime objects beforehand. As it stands both DateTimes in the comparison are recreated for each comparison.

    Going forward, you should always format dates in a readable and sortable fashion, eg: YYYY-MM-DD hh:mm:ss, ideally ISO8601.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法