-- srt is stable: elements (or items with equal keys, for the key-function
-- form) keep their relative input order. The typical use is merging log
-- streams that share a timestamp — events with equal `ts` keep their per-
-- source order inside each tie group, so causality is preserved.
--
-- The pin below sorts a list of original indices by a parallel key list.
-- keys = [2, 1, 2, 1, 2]
-- index order = [0, 1, 2, 3, 4]
-- Items with key=1 are indices [1, 3]; items with key=2 are [0, 2, 4].
-- A stable sort puts the key=1 group first and preserves the original
-- index order inside each group: [1, 3, 0, 2, 4].
by-key xs:L n keys:L n>L n;srt (i:n>n;at keys i) xs
main>L n
by-key [0 1 2 3 4] [2 1 2 1 2]
-- All-equal keys: the output must be the input unchanged.
all-equal xs:L n>L n;srt (x:n>n;0) xs
flat-list>L n;all-equal [4 3 2 1 0]
-- run: main
-- out: [1, 3, 0, 2, 4]
-- run: flat-list
-- out: [4, 3, 2, 1, 0]