0

I am developing a news form where you can enter text and autor and submit it to a blog. After submit a file is created with the following naming convention: newsCounter_date.html, e.g. 121_01_06_2018-093334.html

newsCounter is a value which I increment after every submit, date is the current date.

If someone loads the blog then all news entries are loaded via php, but first they are getting sorted so the latest news entry is on top. I am sorting like this:

$allFiles = array_diff(
    scandir("news", SCANDIR_SORT_DESCENDING), 
    array('..', '.')
);

Everything worked fine, until I reached the 10. blog post, then something weird happened. Instead of the 10. post appearing at the top, it appeared somewhere at the bottom.

I noticed that the sorting does not work anymore, and it does behave completly else in my IDE than in windows. Look at this:

enter image description here

On Windows it goes from 0 -> 12, but in my NetBeans IDE it goes like this: 0 -> 10 -> 11 -> 12 -> 2 -> 3 -> 4 ... so I think NetBeans sort does work like SCANDIR_SORT

Hint: 1 is missing.

Is there another sort method than SCANDIR_SORT_DESCENDING which works like the windows sort?

How can I solve this problem?

Black
  • 18,150
  • 39
  • 158
  • 271
  • Does there have to be a numeric prefix? If you just changed the file names to `Y-m-d H:m:s` I believe the sorting would sort itself out. – Ethan Jan 06 '18 at 21:01
  • 1
    I concur with @david- you should be setting filename structure to utilize a full date format or timestamp to work with an alpha-numeric sort. It appears that your Windows file explorer is showing files chronologically, while NetBeans is in fact showing them alphabetically. In your PHP application, it'll be easiest to load alpha-numerically ascending. Assuming you don't have an overwhelming number of files, you can easily rename individually to the new naming convention; otherwise you can look into writing a bash script or php script to parse each filename and resave. – Anson W Han Jan 06 '18 at 21:11
  • 1
    natsort will also give the desired effect, but requires you to always view the file directory in any file explorer in chronological sorting order. Otherwise, for future reference, always consider the number of characters a number may increment too and include leading zeroes if you must use the incrementer to prefix or suffix a filename. – Anson W Han Jan 06 '18 at 21:14

2 Answers2

1

You can use natsort or sort_numeric. sort_numeric will break if you ever have names with no numbers. natsort will sort the numbers "naturally." Like in this answer. This might be a way to do it without changing date structure.

ilovecoffee
  • 66
  • 1
  • 3
1

Do you have to use a numeric prefix?

If you just changed the file names to Y-m-d H:m:s.html I believe the order would sort itself out (because both alphabetical and natural sorting would order this the same way) e.g.:

  • 2018-01-06 09:04:37.html
  • 2018-01-06 02:34:04.html
  • 2018-01-05 22:01:54.html

(Note that you'd have to use 24 hour format)

Your problem is that some programs order your files "naturally", while others sort them alphabetically. Changing your filenames to the format I suggested avoids this whole problem altogether.

The PHP documentation on natsort highlights the difference quite well:

Standard sorting
Array
(
    [3] => img1.png
    [1] => img10.png
    [0] => img12.png
    [2] => img2.png
)

Natural order sorting
Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)
Ethan
  • 4,295
  • 4
  • 25
  • 44