Skip to content

stdlib: add stable sort and sortBy to lists - #258

Merged
vito merged 3 commits into
mainfrom
stdlib-sort-by
Nov 10, 2026
Merged

vito merged 3 commits into
mainfrom
stdlib-sort-by

Conversation

@vito

@vito vito commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Merge together with #272, which replaces this PR's BuiltinDef.Check hook with the Ordered type-parameter bound. Don't release #258 without it.

Adds [T]!.sort, [T]!.sortBy { x => key } and [T]!.reverse.

Motivation: dagger/dagger.io#5802 had to hand-write a merge sort (stableOrder/sortIndices/mergeIndices) and encode compound keys as strings (toString(d.length).padLeft(12) + d, toString(roleIndex(role)) + key) because Dang has no sort. With this it's docs.sortBy { d => [d.length] } / entries.sortBy { e => {{role: e.role, uri: e.uri}} }.

Semantics

  • Stable (slices.SortStableFunc): equal keys keep input order. Both take descending: Boolean = false, which stays stable (unlike .sort.reverse). sortBy computes each key once.
  • Orderable keys: Int!/Float! numerically; String! by bytes (same as <); Boolean! false first; enums in declaration order (so roleIndex helpers go away); custom scalars by string form (numerically when the scalar's value is a JSON number); lists lexicographically (prefix first); anonymous {{ }} records field by field in written order. Use a list for a same-typed compound key, a record for a mixed-type one.
  • Compile-time rejection of objects, nullable keys (advice: ??), rigid generics, unions and decoded data, through a new BuiltinDef.Check hook. List methods run it in Select.infer (what the receiver resolves) and again in FunCall.inferFunctionType once arguments and block resolve the key.

Maintainer decisions

  1. Record field order: kept. Record keys compare in each literal's written order, and comparing records whose literals wrote their fields in different orders raises at runtime. The written-order change to record literals landed separately in records: keep {{ }} fields in the order written #261.
  2. Runtime-only key checks: accepted. When a key's type isn't settled where the call is written (a reduce([]) accumulator, or &xs.sortBy called later), the key is checked when the sort runs; the docs say so.
  3. descending: kept, since reversing a stable sort reverses ties.
  4. No version gate: these are pure additions.

Docs: docs/lit/data/collections.md (new Sorting section) and the dang-language skill's stdlib reference.
Tests: tests/test_list_sort.dang, five error goldens (two of them reject .sort and .sortBy on Decoded data: list_sort_decoded_element, list_sort_by_decoded_key), and a Go unit test for numeric scalars. analysis.TestMembersOfAList now probes a missing member with List.nope, since List.sort exists.

Add [T]!.sort, [T]!.sortBy { x => key } and [T]!.reverse. Both sorts are
stable (slices.SortStableFunc) and take descending: true, which stays
stable; sortBy computes each key once.

Keys order numerically (Int!, Float!, and scalars whose value is a number),
by bytes (String!, like <, and other scalars), false before true, enums in
declaration order, lists lexicographically and {{ }} records field by field
in the order written, so a compound key is a list when its parts share a
type and a record when they don't. Record types are structural, so two
records of one type may write their fields in different orders; comparing
those raises rather than pick an order that depends on which operand came
first.

Anything else (objects, nullable keys, a generic a, a union) is rejected at
type-check time through a new BuiltinDef.Check hook, which list methods
run on their signature once the receiver, arguments and block resolve it.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: accde93
Status: ✅  Deploy successful!
Preview URL: https://ed335967.dang-3kk.pages.dev
Branch Preview URL: https://stdlib-sort-by.dang-3kk.pages.dev

View logs

@vito
vito changed the base branch from main to record-literal-order November 8, 2026 15:47
@vito
vito changed the base branch from record-literal-order to main November 8, 2026 22:10
@vito
vito added this pull request to stack #273 November 8, 2026 23:04
vito added a commit that referenced this pull request Nov 8, 2026
Replace #258's BuiltinDef.Check hook, and the Select.callCheck plumbing that
ran it, with the Ordered bound in the methods' signatures: sort's element
variable is Ordered, and sortBy's block returns a k! with k Ordered. The
bound is what user code gets from `[a: Ordered]`, so the builtins and a
user's own sort are checked by one mechanism, with whyUnordered as the
predicate.

A bounded element variable takes its receiver as [a!]!: orderedElems checks
the elements on selection, and bounds them instead when their type isn't
known yet, for good when they are a binding's. The key's bound now travels
with the method's type, so `&xs.sortBy` called later is checked at type-check
time too, where #258 left it to the implementation at runtime.

A block whose result may be null where a non-null one is expected (a
nullable sort key) now says so, rather than naming the two types.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 8, 2026
Merging two types (if/else branches, repeated bindings) fell back to a
common supertype or a union when a bound failed, losing the reason: a
sorted `[]` met with a list of objects widened to a union that failed later
with an internal-looking error, and argument merges said only that the lists
didn't unify. mergeTypes now returns the ConstraintError, and a conditional
reports it at the else branch.

A sortBy key that mixes types across branches failed to unify with k! before
the bound was ever checked, so it was reported as not matching a variable the
user never wrote; the block now reports the bound's reason ("it mixes types;
return one type from every branch"), as #258's check did.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 9, 2026
Replace #258's BuiltinDef.Check hook, and the Select.callCheck plumbing that
ran it, with the Ordered bound in the methods' signatures: sort's element
variable is Ordered, and sortBy's block returns a k! with k Ordered. The
bound is what user code gets from `[a: Ordered]`, so the builtins and a
user's own sort are checked by one mechanism, with whyUnordered as the
predicate.

A bounded element variable takes its receiver as [a!]!: orderedElems checks
the elements on selection, and bounds them instead when their type isn't
known yet, for good when they are a binding's. The key's bound now travels
with the method's type, so `&xs.sortBy` called later is checked at type-check
time too, where #258 left it to the implementation at runtime.

A block whose result may be null where a non-null one is expected (a
nullable sort key) now says so, rather than naming the two types.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 9, 2026
Merging two types (if/else branches, repeated bindings) fell back to a
common supertype or a union when a bound failed, losing the reason: a
sorted `[]` met with a list of objects widened to a union that failed later
with an internal-looking error, and argument merges said only that the lists
didn't unify. mergeTypes now returns the ConstraintError, and a conditional
reports it at the else branch.

A sortBy key that mixes types across branches failed to unify with k! before
the bound was ever checked, so it was reported as not matching a variable the
user never wrote; the block now reports the bound's reason ("it mixes types;
return one type from every branch"), as #258's check did.

Signed-off-by: Alex Suraci <[email protected]>
vito added 2 commits November 9, 2026 23:01
Pin that .sort over a list of Decoded data and .sortBy with a Decoded key
are compile errors.

Signed-off-by: Alex Suraci <[email protected]>
The members test used List.sort as a member that doesn't exist; lists
have sort now.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 10, 2026
Replace #258's BuiltinDef.Check hook, and the Select.callCheck plumbing that
ran it, with the Ordered bound in the methods' signatures: sort's element
variable is Ordered, and sortBy's block returns a k! with k Ordered. The
bound is what user code gets from `[a: Ordered]`, so the builtins and a
user's own sort are checked by one mechanism, with whyUnordered as the
predicate.

A bounded element variable takes its receiver as [a!]!: orderedElems checks
the elements on selection, and bounds them instead when their type isn't
known yet, for good when they are a binding's. The key's bound now travels
with the method's type, so `&xs.sortBy` called later is checked at type-check
time too, where #258 left it to the implementation at runtime.

A block whose result may be null where a non-null one is expected (a
nullable sort key) now says so, rather than naming the two types.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 10, 2026
Merging two types (if/else branches, repeated bindings) fell back to a
common supertype or a union when a bound failed, losing the reason: a
sorted `[]` met with a list of objects widened to a union that failed later
with an internal-looking error, and argument merges said only that the lists
didn't unify. mergeTypes now returns the ConstraintError, and a conditional
reports it at the else branch.

A sortBy key that mixes types across branches failed to unify with k! before
the bound was ever checked, so it was reported as not matching a variable the
user never wrote; the block now reports the bound's reason ("it mixes types;
return one type from every branch"), as #258's check did.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 2335c6a into main Nov 10, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant