method
List.sort_with
List.sort_with(s, cmp) -> void
Sorts s in place with a full comparator cmp(a, b) -> int: return a negative number when a should come before b, positive when after, and zero when they tie. Reach for this when the order is not a single key — a multi-field sort (by rarity, then by name), or a comparison that mixes fields. For the common "subtract two numbers" comparator, returning a.field - b.field gives ascending order. The sort is stable (O(n log n) merge sort), so ties (a zero result) keep the elements' prior order.
Example
# doc-check: skip — illustrative; `cmp` is any fn(a, b) -> int in scope
function by_price(a: Item, b: Item) -> int { return a.price - b.price }
program Demo {
handler Step phase Update {
List.sort_with(shop, by_price) # cheapest first
}
}