Repository navigation
stdlib: add stable sort and sortBy to lists - #258
Merged
Merged
Conversation
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]>
Deploying dang with
|
| 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 |
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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sdocs.sortBy { d => [d.length] }/entries.sortBy { e => {{role: e.role, uri: e.uri}} }.Semantics
slices.SortStableFunc): equal keys keep input order. Both takedescending: Boolean = false, which stays stable (unlike.sort.reverse).sortBycomputes each key once.Int!/Float!numerically;String!by bytes (same as<);Boolean!false first; enums in declaration order (soroleIndexhelpers 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.??), rigid generics, unions and decoded data, through a newBuiltinDef.Checkhook. List methods run it inSelect.infer(what the receiver resolves) and again inFunCall.inferFunctionTypeonce arguments and block resolve the key.Maintainer decisions
reduce([])accumulator, or&xs.sortBycalled later), the key is checked when the sort runs; the docs say so.descending: kept, since reversing a stable sort reverses ties.Docs:
docs/lit/data/collections.md(new Sorting section) and thedang-languageskill's stdlib reference.Tests:
tests/test_list_sort.dang, five error goldens (two of them reject.sortand.sortByonDecodeddata:list_sort_decoded_element,list_sort_by_decoded_key), and a Go unit test for numeric scalars.analysis.TestMembersOfAListnow probes a missing member withList.nope, sinceList.sortexists.