自然排序scandir的结果? [英] Naturally sorting results of a scandir?

查看:84
本文介绍了自然排序scandir的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对包含scandir函数结果的数组进行排序。我已经尝试过使用natsort()php函数,但是它似乎无法正常工作,因为我需要将其用于目录。

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.

扫描的内容目录假定 HH(Z)DDMonYYYY 命名约定。在数组上使用natsort函数后,结果是这样的:

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", ...]

如您所见,该函数正在计算前两位数字并使用它们进行排序,但忽略了日期(23、18、17, 16)每个名称中。

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"]

由于目录是按顺序创建的,因此我意识到我可以按目录创建或修改时间排序,并且在99%的时间内都可以。但是,在极少数情况下,目录修改时间并不是很完美,我想避免出现这种情况。

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.

有没有办法无需使用修改或创建时间即可完成php中的目标?

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

感谢所有人!

编辑:对于上下文,我正在使用Python脚本写入这些目录并对其执行简单的操作。 Python为那些不知道的人提供了一个名为 natsorted的程序包,它可以毫无问题地对上述示例数组中的目录进行排序。只是想知道是否还有一个简单的php解决方案,然后再开始增加复杂性。

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.

推荐答案

所有这些 natsort()的目的是尝试处理具有任意长度数字序列的字符串排序,它不能神奇地解释奇异的日期格式。甚至要做试图找出日期的PHP函数也无法解决这一问题,因为它们实际上只是使用了预定义的常用格式列表,甚至 then 都是有问题的

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.

恕我直言,您应始终使用类似 DateTime :: createFromFormat() 和一个显式格式字符串。

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

输出:

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

这对于少量日期和/或不经常调用的日期将足够工作。但是,如果您要对大量日期进行排序或经常对它们进行排序,则需要预先创建DateTime对象。按照目前的情况,比较中的两个DateTime都会为每个比较重新创建

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.

展望未来,您应始终将格式设置为可读且可排序时尚,例如: YYYY-MM-DD hh:mm:ss ,最好是 ISO8601

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

这篇关于自然排序scandir的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆