diff --git a/src/domain/analysisView/environmentStateWriteExplain.test.ts b/src/domain/analysisView/environmentStateWriteExplain.test.ts index 2a7415001b..00ce524ae9 100644 --- a/src/domain/analysisView/environmentStateWriteExplain.test.ts +++ b/src/domain/analysisView/environmentStateWriteExplain.test.ts @@ -553,6 +553,7 @@ it.each<[JsonValue | undefined, JsonValue | undefined, number | undefined]>([ ["10", 9, 1], ["7", 7, 0], ["2026-03-04T05:00:00Z", "2026-03-04T06:30:00+02:00", 1], + ["2026-03-04T05:00:00.123000001Z", "2026-03-04T05:00:00.123000002Z", -1], [Date.UTC(2026, 2, 4, 6) / 1000, "2026-03-04T05:00:00Z", 1], ["apple", "banana", -1], ["Zebra", "apple", -1], @@ -583,6 +584,26 @@ it("offers each strictly ordered field in its direction, then last and first", ( expect(insertCandidates([], ["id"])).toEqual(fallback); }); +it("uses retained submillisecond precision to recognize and place sorted timestamps", () => { + const members = [ + { at: "2026-03-04T05:00:00.123000002Z" }, + { at: "2026-03-04T05:00:00.123000003Z" }, + ]; + const rule: InsertRule = { + rule: "sorted", + field: "at", + direction: "ascending", + }; + expect(insertCandidates(members, ["at"])).toEqual([ + rule, + { rule: "last" }, + { rule: "first" }, + ]); + expect( + insertIndex(rule, members, { at: "2026-03-04T05:00:00.123000001Z" }), + ).toBe(0); +}); + const ASCENDING = [{ n: 1 }, { n: 3 }, { n: 5 }]; const DESCENDING = [{ n: 5 }, { n: 3 }, { n: 1 }]; const up: InsertRule = { rule: "sorted", field: "n", direction: "ascending" }; diff --git a/src/domain/analysisView/environmentStateWriteExplain.ts b/src/domain/analysisView/environmentStateWriteExplain.ts index 5aad6c4d77..63f92b5cab 100644 --- a/src/domain/analysisView/environmentStateWriteExplain.ts +++ b/src/domain/analysisView/environmentStateWriteExplain.ts @@ -431,7 +431,8 @@ export const compareValues = ( const [a, b] = [integerOf(left), integerOf(right)]; if (a !== undefined && b !== undefined) return a - b; const [c, d] = [readClock(left), readClock(right)]; - if (c !== undefined && d !== undefined) return c.ms - d.ms; + if (c !== undefined && d !== undefined) + return c.ms - d.ms || c.subMillisecond - d.subMillisecond; return typeof left === "string" && typeof right === "string" ? compare(left, right) : undefined;