A robot event log can contain several records with the same timestamp. A useful playback order might put time first, then use a controller's sequence number for events recorded at the same tick. In NumPy, the order in which keys are passed to lexsort determines which field takes precedence. A job can produce a valid permutation of every row while sorting on the wrong primary key.
The example below was checked with NumPy 2.5.2. The timestamps, sequence numbers, and event labels are invented. This is an offline event table. A display ordering based on these fields does not establish the physical order of actions measured on different devices.
Pass the primary key last
The fixture has five events labeled A through E. Their timestamps are 102, 101, 102, 101, and 102 in the order they entered this batch. Their sequence numbers are 9, 8, 8, 9, and 9. The sequence is a secondary sorting key in this example. The timestamp should take precedence even when two events in different timestamp groups have the same sequence number.
import numpy as np
time = np.array([102, 101, 102, 101, 102])
sequence = np.array([9, 8, 8, 9, 9])
label = np.array(["A", "B", "C", "D", "E"])
by_time = np.lexsort((sequence, time))
assert label[by_time].tolist() == ["B", "D", "C", "A", "E"]
NumPy's lexsort returns indices that describe the sorted order. It uses the last key in the tuple first. Time 101 comes before time 102. Within time 101, B has sequence 8 and precedes D at sequence 9. Within time 102, C has sequence 8 and comes before A and E. Those last two events share both supplied keys, so their order follows the input order.
by_sequence = np.lexsort((time, sequence))
assert label[by_sequence].tolist() == ["B", "C", "D", "A", "E"]
In the second result, C has time 102 but precedes D at time 101. The order is correct for the different question of sorting by sequence first. A test that only checks whether the result contains all five events would accept both orders. Include a pair of records where reversing the priority changes their order.
Keep every field on the same permutation
Use the returned indices to gather timestamps, payloads, sequence numbers, and event identifiers from their original arrays. Sorting each column independently can attach a valid value to the wrong event. Check that each parallel array was built from the same row ordering before applying the indices. When reading a large table, sorting structured records or applying one shared permutation preserves that association.
NumPy's argsort also returns an index order. Calling it on an ordinary two-dimensional array of key rows sorts along an axis and can return separate orders for each row. That is a different operation from computing a single multi-key order for events. If using argsort with structured fields, specify the field comparison order and verify the tie policy for any other fields in the record.
A two-pass check can independently reproduce the sort. First build the order by the secondary key. Then stably sort the timestamps as they occur in that ordered view. Since the second pass preserves the relative order within each equal-timestamp group, it keeps the sequence order arranged by the first pass.
sequence_first = np.argsort(sequence, stable=True)
time_second = np.argsort(time[sequence_first], stable=True)
two_pass = sequence_first[time_second]
assert np.array_equal(two_pass, by_time)
Both passes in the check preserve earlier order for ties. Applying the passes in reverse would promote sequence to the primary key and would answer the wrong question for the playback.
Distinguish stable sorting from an input-independent order
A stable sort preserves the input order of records whose comparison keys are equal. Suppose a loader reads E before A on the next run. They still tie on time and sequence, so the stable sort keeps E first. That behavior is desirable when the input position is part of the declared order. It does not create an independent tie rule across different loading orders.
load_order = np.array([4, 1, 2, 3, 0])
tied_order = np.lexsort((sequence[load_order], time[load_order]))
assert label[load_order][tied_order].tolist() == [
"B", "D", "C", "E", "A",
]
If a report must consistently order fully tied records, add a persistent final key. This example uses distinct synthetic event IDs. The ID is passed first, making it the key considered after time and sequence. Changing how the rows arrive does not change the resulting order in this fixture.
event_id = np.array([31, 14, 22, 15, 30])
key_order = np.lexsort((event_id, sequence, time))
reordered = np.lexsort((
event_id[load_order], sequence[load_order], time[load_order]
))
assert np.array_equal(key_order, load_order[reordered])
assert label[key_order].tolist() == ["B", "D", "C", "E", "A"]
An event ID can make a report deterministic without implying that the lower ID physically happened first. IDs can be unique only within a source, and sequence counters can reset or roll over. Preserve the source and acquisition session when those fields matter. Resolve clocks, units, and missing timestamps under a separate input contract before drawing timing conclusions from a merged log.
Sorting also leaves repeated records in the table. If the logger can replay an event during recovery, detect duplicates using the acquisition identity before interpreting the sorted rows as separate events.
For a replay regression, validate the primary order across distinct timestamps and secondary order among equal timestamps. Also reverse the input order of two fully tied records and check that the result follows the intended policy. These checks explain why rows appear in a particular order and protect their payload association when the input batch changes.
Sources checked November 28, 2026
NumPy, numpy.lexsort
https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html
NumPy, numpy.argsort
https://numpy.org/doc/stable/reference/generated/numpy.argsort.html
Illustration. AI-generated concept showing time as the primary key and sequence as a tie-break key for robot event records.