Fix NaN from NEGATIVE_INFINITY subtraction in sortThreadsForSidebar

When two threads both yield Number.NEGATIVE_INFINITY timestamps, the
subtraction (-Infinity) - (-Infinity) produces NaN, which bypasses the
ID-based tiebreaker and causes unpredictable sort order.

Use explicit comparison (=== / >) instead of subtraction, matching the
pattern already used in sortProjectsForSidebar.
This commit is contained in:
Cursor Agent committed 2026-03-24 19:10:49 +00:00
1 parent 1f96d6da92
commit e09809f495
1 file changed
+3 -1
+3 -1
View File
@@ -270,8 +270,10 @@ export function sortThreadsForSidebar<
T extends Pick<Thread, "id" | "createdAt" | "updatedAt" | "messages">,
>(threads: readonly T[], sortOrder: SidebarThreadSortOrder): T[] {
return threads.toSorted((left, right) => {
const rightTimestamp = getThreadSortTimestamp(right, sortOrder);
const leftTimestamp = getThreadSortTimestamp(left, sortOrder);
const byTimestamp =
getThreadSortTimestamp(right, sortOrder) - getThreadSortTimestamp(left, sortOrder);
rightTimestamp === leftTimestamp ? 0 : rightTimestamp > leftTimestamp ? 1 : -1;
if (byTimestamp !== 0) return byTimestamp;
return right.id.localeCompare(left.id);
});