Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

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("HdMY",$a) <=> DateTime::createFromFormat("HdMY",$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...