Skip to content

Conversation

@kagahd
Copy link
Contributor

@kagahd kagahd commented Jun 11, 2025

As written in #988 blog notes of older photos (before 1970-04-27) are wrongly sorted shown.
The reason becomes obvious when looking at the numerical presentation of the dates:

  • 1970-04-26 = 9936000000
  • 1970-04-27 = 10022400000
  • 1970-11-01 = 26265600000

I first suspected the culprit in the “10-digit rule” which is to distinguish seconds from milliseconds when normalizing timestamps. In many systems, a timestamp value less than 10^10 is assumed to be in seconds, whereas a larger value is assumed to be in milliseconds. But this assumption did not hold true since PiGallery2 uses dates properly transformed to milliseconds.
The reason is even simpler, but well hidden in the blog.service.ts.
The line dates.sort(); does not look suspicious at all unless you know that the JavaScript sort() method sorts values lexicographically (as strings) rather than numerically even if dates is an array of type number. This leads to incorrect sorting of timestamps. The solution is to provide a numeric comparison function like sort((a, b) => a - b) to ensure proper chronological sorting, which this PR has implemented.

@bpatrik
Copy link
Owner

bpatrik commented Jul 19, 2025

ah nice finding. Thanks!

@bpatrik bpatrik merged commit fc191f4 into bpatrik:master Jul 19, 2025
4 of 5 checks passed
@bpatrik bpatrik added the bug label Jul 19, 2025
@bpatrik bpatrik added this to the Next (probably v2.5) milestone Jul 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants