Proposal: sorting toolkit — sort_by / sort_desc_by / sort_with, stable & O(n log n), for records and queries #11
Labels
No labels
area:ci
area:docs
area:input
area:net
area:rendering
area:repo
area:stdlib
area:tooling
area:types
cleanup
dx
priority:high
priority:low
priority:medium
proposal
status:in-progress
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: workshopsoft/ludic#11
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Grow sorting from the current single primitive into a small, game-friendly
sorting toolkit: sort by a key or comparator, sort records/entities
(not just numbers), ascending or descending, stable, and backed by an
efficient algorithm.
Current state
List.sorttoday is an in-place ascending insertion sort over numericelements only (
selfhost/emit_list.ludic). That's O(n²) and can't sort a listof structs by a field, which is what games usually need.
Why it matters for game devs
yor layer every frame.These are everyday tasks; today they force the developer to hand-roll a sort.
Proposed API (illustrative)
sort_by(ascending by a key),sort_desc_by,sort_with(comparator).Considerations
a stable Tim/merge hybrid) for large lists; keep insertion sort as the small-n
fast path.
tied to memory).
property/record types.Scope / acceptance
sort_by,sort_desc_by,sort_within the self-host compiler.Related: #1 (closures/type system), #2 (List namespace).
Shipped in
4aa2012.Sorting toolkit — grows List sorting from the numeric-only insertion sort into a small, game-friendly toolkit:
List.sort_by(s, keyfn)— ascending by a key (draw order, inventory by price)List.sort_desc_by(s, keyfn)— descending (leaderboards)List.sort_with(s, cmpfn)— fullcmp(a, b) -> intcomparator (multi-field sorts)Comparators/keys are passed as named top-level functions rather than inline lambdas, so the toolkit ships now without waiting on closures (#1); when #1 lands, lambda selectors can be added on top with no API change.
Engine: a stable, bottom-up merge sort — O(n log n), O(n) scratch, deterministic. A single predicate decides stability ("take the right run's head only on a strict win"), so equal keys keep their prior order — the tie-break leaderboards and z-sorting need.
List.sortis now a hybrid: insertion sort for small n (<32), merge sort above; both stable, so output is identical and existing golden renders are unchanged.Sorts records and query results (record slices hold pointer elements, so the key/comparator receives the record). Key functions must return a numeric type (int/fixed/long).
Acceptance: sort_by/sort_desc_by/sort_with ✓ · stable O(n log n) ✓ · records & query results ✓ · docs + leaderboard/draw-order examples ✓ · tests incl. stability ✓. All suites green (28 self-host / 46 test / 29 test-tools); reseeded, C-free bootstrap fixpoint holds.