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