mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-26 06:34:20 +08:00
A comparator that chains key differences with `||`, such as `(b.score ?? 0) - (a.score ?? 0) || a.id - b.id`, ran through the comparator sort: TimSort called the closure once per comparison. Each term orders one number field, ascending or descending, and a term that is 0 or NaN falls through to the next, so the chain orders elements by their keys with the first deciding. With no key NaN or -0 that order is consistent, and a stable sort by the keys gives the comparator sort's permutation. The recognizer now accepts such chains of up to eight terms over number fields of records and class instances, and a key may read an optional number field with a number default (`x.k ?? 0`), also as a single key. The runtime reads and checks every key of every element before it moves a slot, then runs stable keyed passes from the last key to the first. A NaN or -0 key, or a chain with any other term, keeps the comparator sort.