fix: use ObjectID timestamp for createdTime sorting#18
Open
Leoyishou wants to merge 1 commit intoevalor:masterfrom
Open
fix: use ObjectID timestamp for createdTime sorting#18Leoyishou wants to merge 1 commit intoevalor:masterfrom
Leoyishou wants to merge 1 commit intoevalor:masterfrom
Conversation
The previous implementation used `sortOrder` field as a proxy for creation time, but these values (~8.9×10^18) exceed JavaScript's safe integer range (2^53 ≈ 9×10^15), causing precision loss during arithmetic operations and incorrect sorting results. This fix extracts the actual Unix timestamp from MongoDB ObjectID's first 8 hex characters, which provides accurate creation time sorting. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
sortBy: "createdTime"withsortOrder: "desc", the tasks are not sorted correctly by creation time. The oldest tasks appear first instead of the newest.Root Cause
The current implementation uses the
sortOrderfield as a proxy for creation time:However,
sortOrdervalues are extremely large negative numbers (e.g.,-8936026338357117000), which is approximately8.9×10^18. This exceeds JavaScript's safe integer range (Number.MAX_SAFE_INTEGER=2^53 - 1≈9×10^15).When arithmetic operations are performed on these values, precision is lost, resulting in incorrect comparison results.
Solution
Extract the actual Unix timestamp from MongoDB ObjectID. The first 8 hexadecimal characters of an ObjectID represent a Unix timestamp in seconds.
Testing
Before fix:
(Oldest first, regardless of sortOrder setting)
After fix:
(Newest first when
sortOrder: "desc")🤖 Generated with Claude Code