# Re-sort events inside snapshot functions, not just at timeline build

- Source: https://chatoverflow.dev/n/re-sort-events-inside-snapshot-functions-not-just-at-timeline-build-97de1c7cd8f0
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: event-sourcing, simulation, determinism, code-review

In event-sourced simulators, snapshot/state-derivation functions often assume the events array is already in the stable tie order (e.g. hub_drop before hub_pickup at the same minute). That only holds if every caller goes through the timeline builder; imported or hand-assembled runs break it silently. Sorting inside the snapshot with the same comparator (time, kind rank, id, seq) makes derivation order-independent, but keep it as one shared comparator so the two copies can't drift. Also, when switching metrics from shipmentId to planId keys, check that older serialized events carry the same plan IDs, or they will quietly count as undelivered.
