Hello,
I just upgraded from 1.5.7 to 1.5.8 and sorting of images in albums seems broken: next_image() now returns images out of order (I have a custom theme bu it has been working for years, upgraded after upgraded). The expected order is by date descending. It was working until 1.5.7. Could it be related to the use of a context?
Regards,
Mahamane
The problem seems to be in functions.php, sortByKey().
For sorttype=date sortdirection=DESC
Album.sortImageArray() gets the data in the right order from the DB. It turns out that the images name include the date and time, making debuging easier (format=YYMMDD_HHMMSS) =>
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
Album.sortImageArray(): filename=210622_150417_geraniums.jpg
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
Album.sortImageArray(): filename=210607_191609_camelia.jpg
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
Album.sortImageArray(): filename=210607_182848_clematite.jpg
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
Album.sortImageArray(): filename=210607_182727_camelia.jpg
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
Album.sortImageArray(): filename=210607_173926_clematite.jpg
{15894:Wed, 23 Jun 2021 04:32:33 GMT}
The records are storeds in the array $results.
Then $results is sorted by
$results = sortByKey($results, str_replace('`', '', $sortkey), $order);
and then $results is no longer sorted correctly.
Digging further, the problem occurs when calling sortMultiArray() with $natsort = true
Called with false in sortByKey() solves the issue:
$results = sortMultiArray($results, $indicies, $order, true, false, true); -> does not work
$results = sortMultiArray($results, $indicies, $order, false, false, true); -> works
Thanks for investigating. The array sroting has indeed been extended to use "real natural" order if the Collator class is available on your system. The oder especially on non English terms may otherwise not be correct without it as plain PHP natsort does not cover it. I am not sure why this would affect sorting by filename actually.
In any case we will try to reproduce it.
But please tell iif your system has the Collator class available (https://www.php.net/manual/de/class.collator.php)? Because the sorting behaviour is different with and without even for "normal" strings. Sorting by date should also work with it but perhaps it needs an exception for date sorting to use the "standard" natsort.
It is really weird, so far we cannot reproduce any issue with sorting by date (the date stored in the database).
You mention filenames like 210622_150417_geraniums.jpg. Are you perhaps actually sorting by filename. Those numbers would not really work as date sorting, it would have to be internation format YYYY-MM-DD to work properly as "strings".
I am sorry; I tried again and image sorting by date ascending and descending does work for me as intended (with Collator class as well).
Be sure no cache plays tricks on you, e.g. visit the site loggedin and clear your browser cache by force. The latter is very persistent in almost all browsers and a constant source of confusion.
I came here to report the same issue that's appeared after upgrade to 1.5.8 (to solve missing metadata with Multiverse bug!).
Sorting by date is broken across albums that haven't been touched for months (eg https://gallery.steam4me.net/index.php?album=trams/2020) but tried sorting by filename (eg https://gallery.steam4me.net/index.php?album=trams/2021) and that seems to be working ok.
@J_C: Example please, especially what filenames you are using. The Collator class does realy natural ordering if available which is a bit different from the standard natural order PHP provides. That was actualyl raelly wrong for especially some languages (e.g. umauts in my native language German).
@steam4m: I can only tell that I cannot reproduce any issue with sorting by date.
In any case we always need the info if your system supports the collator class or not as that is cruical to investigate anything. Review phpinfo if the intl PHP extension is available or re-run setup to see if it is noted as not available.
All right, I can reproduce the issue now. What does not work is the setting "Options -> Gallery -> Sort gallery by" while the individual setting on an album does.
Please verify this.
COnfused my self, need more coffee…, of course setting album sorting does not work on images ;-)
Opions -> Image -> Sort images by is the global setting and that also works for me as intended.
Sorry guys…
Here’s the subalbum I first noticed it in today. I just changed it from sort by Manual back to Filename, when I did this is the order that came up. I will tell you that when it was on sort by Filename earlier, some of the 2021 files were mixed in with the 2020 (the "Name" and "More info" have been changed to obscure the actual filenames, but I don't think that should affect the sort order since they start with dates).
2020-11-14 - Name - More info.mp4
2020-12-31 - Name - More info.mp4
2020-8-9 - Name - More info.mp4
2020-11-19 - Name - More info.mp4
2020-8-16 - Name - More info.mp4
2020-8-13 - Name - More info.mp4
2020-8-13 - Name - More info.mp4
2020-8-13 - Name - More info.mp4
2020-5 - Name - More info.mp4
2020-4-15 - Name - More info.mp4
2020-8-18 - Name - More info.mp4
2020-12-25 - Name - More info.mp4
2020-12-22 - Name - More info.mp4
2020-12-20 - Name - More info.mp4
2020-12-2 - Name - More info.mp4
2020-12-18 - Name - More info.mp4
2020-12-18 - Name - More info.mp4
2020-12-12 - Name - More info.mp4
2020-11-20 - Name - More info.mp4
2020-12-23 - Name - More info.mp4
2021-2-6 - Name - More info.mp4
2021-6-23 - Name - More info.mp4
2021-6-14 - Name - More info.mp4
2021-5-23 - Name - More info.mp4
2021-5-16 - Name - More info.mp4
2021-3-6 - Name - More info.mp4
2021-3-16 - Name - More info.mp4
2021-1-30 - Name - More info.mp4
2021-1-7 - Name - More info.mp4
2021-1-4 - Name - More info.mp4
2021-1-4 - Name - More info.mp4
2021-1-31 - Name - More info.mp4
2021-1-30 - Name - More info.mp4
2021-1-20 - Name - More info.mp4
2021-1-16 - Name - More info.mp4
2021-6-23 - Name - More info.mp4
The Collator class appears to be off when I tested for it in two languages.
PHP info has this for intl-
intl
Internationalization support enabled
ICU version 69.1
ICU Data version 69.1
ICU TZData version 2021a
ICU Unicode version 13.0
Directive Local Value Master Value
intl.default_locale no value no value
intl.error_level 0 0
intl.use_exceptions 0 0
If you have the intl class the collator class is included.
The problem you are encountering is that your filename - if I understand corrctly
2020-8-9which should be 2022-08-0. This is likely causing the wrong ordering as the real natural order really sortes by that. As the general order is ascending by year, just the months and dates are mixed up. Dates win other formats are never sorted correctly as they are as "strings".If you can try with an album with plain alphabetical names. If that works for you as intended this is in your specific case the reason.
Otherwise we would need to add yet another option so users can choose between natural order and real natural order.
If you like to help, find the function sortArray() function within zp-core/functions.php. there you find the part:
if (class_exists('collator')) {
$locale = getUserLocale();
$collator = new Collator($locale);
if ($case_sensitive) {
$collator->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST);
}
$collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$success = $collator->asort($array);
} else {
if ($case_sensitive) {
$success = natsort($array);
} else {
$success = natcasesort($array);
}
}
Remive the collator if check so it uses always the standard PHP natural order (which often is really wrong in some languages with umlauts, accents and other things):
if ($case_sensitive) {
$success = natsort($array);
} else {
$success = natcasesort($array);
}
We really did a lot tests with this sorting and the wrong ordering of in this case French language terms reported by a user was the reason to add this.