Module
std.sort — ordering a List in place. Stability tier: core.
One algorithm: bottom-up stable merge sort, O(n log n) comparisons and O(n) scratch, stable by construction (a tie takes from the left run). It is not a hybrid and does not degrade to insertion sort on short runs — the constant-factor work belongs to s44's bench tracks, and a second algorithm would need a second stability proof.
The scratch is a fresh List per pass rather than in-place swapping: index assignment (xs[i] = v) executes on one implementation only, and std.list.swap rebuilds the whole list per call (O(n) a swap), so the merge that allocates is the FASTER of the two available shapes as well as the more portable one. Region-scratch placement (.in(r)) is s37's plumbing, named here, not shipped.
Monomorphic and generic, and why each is which. sort_ints, sort_floats and sort_strs are the contract's monomorphic conveniences and they RUN. sort and sort_by are the generic targets: sort[T: Ord] needs Ord.cmp dispatch, which executes in neither implementation, and sort_by's Ordering result cannot be examined outside the module that declares the enum (F-0029) — so both ship as bodies that pass the static ladder and are unsupported at the run rung, with their tests ledgered honestly. is_sorted and is_sorted_by go the other way and ship monomorphic over int: a predicate with no running witness proves nothing, and these two are the witnesses sort_ints's tests use.
Reversal composes rather than multiplying the API: sort ascending and call std.list.reverse, or pass a comparator that swaps its arguments (sort_by(mut xs, reverse_cmp)); there is no sort_desc.
RETURNING TWINS (sc44, wolf-std#23), and the one name that could not be written. The Wolf Book's §5.2 has promised a chain since bs00 — totals.pairs().sorted_by(…).take(2) — and every function here was in place and unit-returning, so the page printed a text fence. Four functions answer the first half: sorted_by beside sort_by, and sorted_ints/sorted_floats/sorted_strs beside the three that run. Each is its in-place twin over a copy, leaves its argument alone, and takes the container in the bare read mode (§8: mut where mutation IS the operation, and here it is not).
**take CANNOT BE WRITTEN AND DOES NOT NEED TO BE**, and both halves are measurements rather than opinions. fn take is E0008: take is a reserved keyword and cannot be a name; wolf has no raw identifiers on BOTH machines — the ownership verb wolf-lang#154 names owns the word outright, so the collision is not a style question. And the prefix it would have provided is the language's: xs[..n] and xs[1..] ([mem.list.slice]) both run on all three lanes at the sc44 pins, measured. A std function shadowing a working builtin with a worse name is the one thing §8's container rule forbids outright. So the chain in std's vocabulary is sort.sorted_ints(xs)[..2], it runs today, and the book can stop fencing it as text.
Bench kernels (D5/D36, report-only). The three registered candidates for s44's T1 perf suite are sort_ints over 1e5 random ints, sort_ints over a sorted run of the same length (the adversarial case a merge sort should win), and std.search.binary_search over 1e6 elements. They are NAMED here rather than registered: the s44 registry does not exist yet and this repository cannot write into it, so the closeout carries the handoff.
BLOCKED(c05), named and bodiless: sort_by_key, sort_unstable, sort_dedup. The first waits on closures (a key extractor is a closure in every real use); the second waits on a second algorithm earning its place; the third belongs to whichever sprint gives std.list.dedup an Eq bound that dispatches.
Functions and methods
fn · Source line 80
pub fn sort[T: cmp.Ord](mut xs: List[T])
Sorts xs ascending in place, stably. Total: no error row, no traps (element comparison is Ord.cmp, which is total by its own law).
Does not execute at these pins — Ord.cmp dispatch is unsupported in both implementations (F-0002) — and the running witnesses are sort_ints, sort_floats and sort_strs, which share this merge's shape line for line. Example is prose for the same reason: a fenced block must run.
fn · Source line 141
pub fn sort_by[T](mut xs: List[T], by: fn(T, T) -> cmp.Ordering)
Sorts xs ascending in place, stably, ordering by by rather than by Ord. by must be a total order or the result is merely a permutation — std cannot check that and does not pretend to.
The comparator is a NAMED function today (sort_by(mut xs, by_age)); closures arrive with c05 and change no signature here. Does not execute at these pins: an Ordering produced by an imported module cannot be examined by this one (F-0029, re-measured at 0.2.14 / 0.1.36 by sc50: no lane). Prose, not a fenced example, for that reason.
The sort that runs is std.list.sort_by(mut xs, le) (sc50): the same mode, the same stable bottom-up merge, on all three lanes, taking a bool relation (le(a, b) reads "a may precede b") instead of an Ordering. The comparator's type is the one difference between the two, and it is stated on both.
fn · Source line 201
pub fn sorted_by[T](xs: List[T], by: fn(T, T) -> cmp.Ordering) -> List[T]
sort_by's returning twin: a fresh list holding xs's elements in by's order, with xs untouched. The chain's sort (wolf-std#23). by must be a total order or the result is merely a permutation. Total: no error row, no traps beyond allocation.
Does not execute at these pins, for the two reasons its twin carries and neither of them new: Ordering produced by std.cmp cannot be examined by this module (F-0029), and the checked tier answers unsupported — trait dispatch on a non-nominal receiver at Ord.cmp(src[b], src[a]). It ships anyway and it ships NOW because the name is what the book promises, and a promised name that does not exist is worse than one that is honestly freight — tests/sort/generic_contract.lu proves the shape typechecks. Prose, not a fenced example, for that reason.
fn · Source line 223
pub fn sort_ints(mut xs: List[int])
Sorts a list of int ascending, in place, stably. O(n log n) comparisons, O(n) scratch. Total: no error row, no traps.
var xs = List[int]()
(mut xs).push(3)
(mut xs).push(1)
(mut xs).push(2)
sort.sort_ints(mut xs)
xs[0] == 1
xs[1] == 2
xs[2] == 3
fn · Source line 318
pub fn sort_floats(mut xs: List[f64])
Sorts a list of f64 ascending, in place, stably, under IEEE totalOrder (see total_lt): every NaN lands at the end and -0.0 sorts strictly below +0.0. Total: no error row, no traps.
var xs = List[f64]()
(mut xs).push(2.5)
(mut xs).push(-1.0)
(mut xs).push(0.0)
sort.sort_floats(mut xs)
xs[0] == -1.0
xs[1] == 0.0
xs[2] == 2.5
fn · Source line 383
pub fn sort_strs(mut xs: List[str])
Sorts a list of str ascending, in place, stably, in the language's byte-lexicographic order ([mem.str.order]): unsigned byte compare, shorter first on a shared prefix, so every multi-byte code point sorts above every ASCII one. That is NOT alphabetical order in any human language and this module does not pretend it is — collation is a table question (F-0019) and belongs where the tables land.
Total: no error row, no traps. Runs on the interpreter only at these pins: < on str is unsupported in wolfc's checked tier and in the native rung's string lowering (F-0018's family).
var xs = List[str]()
(mut xs).push("pear")
(mut xs).push("apple")
sort.sort_strs(mut xs)
xs[0] == "apple"
xs[1] == "pear"
fn · Source line 444
pub fn sorted_ints(xs: List[int]) -> List[int]
sort_ints's returning twin: a fresh ascending list, xs untouched. Total: no error row, no traps beyond allocation.
This is the chain the book's §5.2 wants, with the prefix spelled as the language's slice: sort.sorted_ints(xs)[..2].
var xs = List[int]()
(mut xs).push(3)
(mut xs).push(1)
(mut xs).push(2)
sort.sorted_ints(xs)[0] == 1
sort.sorted_ints(xs)[..2].len == 2
xs[0] == 3
fn · Source line 463
pub fn sorted_floats(xs: List[f64]) -> List[f64]
sort_floats's returning twin, under the same IEEE totalOrder. Total: no error row, no traps beyond allocation.
var xs = List[f64]()
(mut xs).push(2.5)
(mut xs).push(-1.0)
sort.sorted_floats(xs)[0] == -1.0
xs[0] == 2.5
fn · Source line 485
pub fn sorted_strs(xs: List[str]) -> List[str]
sort_strs's returning twin, in the language's byte-lexicographic order ([mem.str.order]). Total: no error row, no traps beyond allocation. Runs on the interpreter only at these pins, for its twin's reason: < on str is unsupported in the native rung's string lowering.
var xs = List[str]()
(mut xs).push("pear")
(mut xs).push("apple")
sort.sorted_strs(xs)[0] == "apple"
xs[0] == "pear"
fn · Source line 507
pub fn is_sorted(xs: List[int]) -> bool
True when xs is in ascending order (ties allowed). The empty list and every one-element list are sorted. Total: no error row, no traps.
Monomorphic over int at these pins — see the module header for why the predicates go one way and the sorts the other.
var xs = List[int]()
(mut xs).push(1)
(mut xs).push(1)
(mut xs).push(2)
sort.is_sorted(xs) == true
fn · Source line 527
pub fn is_sorted_by(xs: List[int], by: fn(int, int) -> cmp.Ordering) -> bool
True when xs is in ascending order under by (ties allowed). Total: no error row, no traps.
The comparator answers std.cmp.Ordering, so this function's own examination of that answer is a cross-module Ordering read and does not execute at these pins (F-0029); the static ladder is clean and the test is ledgered unsupported on both lanes. Prose, not a fenced example, for that reason.