diff --git a/src/tools/task/list-tasks.ts b/src/tools/task/list-tasks.ts index 51989cd..b274600 100644 --- a/src/tools/task/list-tasks.ts +++ b/src/tools/task/list-tasks.ts @@ -153,10 +153,19 @@ function sortTasks( break; } case "createdTime": { - // Use sortOrder as proxy for creation time - const orderA = a.sortOrder ?? 0; - const orderB = b.sortOrder ?? 0; - comparison = orderA - orderB; + // Extract timestamp from MongoDB ObjectID (first 8 hex chars) + // The sortOrder field values exceed JavaScript's safe integer range (2^53), + // causing precision loss during arithmetic operations. + // ObjectID's first 8 hex characters represent a Unix timestamp. + const getTimestampFromId = (id: string | undefined): number => { + if (typeof id === 'string' && id.length >= 8) { + return parseInt(id.substring(0, 8), 16); + } + return 0; + }; + const timeA = getTimestampFromId(a.id); + const timeB = getTimestampFromId(b.id); + comparison = timeA - timeB; break; } }