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.