Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
0.2.0 Preview 1 · Frozen documentation

CallableSort

Sorts a writable slice through one owned concrete safe comparator.

Sn is under heavy development. Reference extracted from this release’s bundled standard library. Public API examples are not an exhaustive compatibility test. Read the matching release guide for tested workflows and limitations. Read the status →

Module std.iteration

import std.iteration;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

Sort

pub fn Sort<T, C>(Values: mut T[], mut Compare: C)
    where T: Movable, MoveAssignable
    where C: BinaryCallable<readonly T&, readonly T&, i32>, Movable

Sorts a writable slice through one owned concrete safe comparator.

Iterative unstable heapsort uses O(n log n) comparisons/moves in the worst case and O(1) additional storage. The callback owns its concrete environment; each heap restoration borrows that same environment. Element swaps use safe selected move construction/assignment, supporting non-Copy elements. No box, recursion, allocator or synchronization is added. Serialize mutation and keep captured backing alive; callback and lifecycle costs remain additional work. T: Movable/MoveAssignable element type. C: Concrete Movable safe readonly borrowed binary comparator. Values: Writable contiguous storage, protected from conflicting access. Compare: Owned callback; explicitly move non-Copy named callback state. Pre: Compare gives a consistent strict weak ordering without invalidating Values. Warning: Element addresses remain fixed but logical identities change; no trap rollback. Return: No value after ordering Values according to Compare; equal elements have no stability guarantee.

Example

module example.concrete_sort;
from std.iteration import Sort, BinarySearch;
fn Main() -> i32 {
    let Direction: i32 = -1;
    let Descending = (Left: readonly i32&, Right: readonly i32&) => {
        if Left < Right { return -Direction; }
        if Left > Right { return Direction; }
        return 0;
    };
    var Values: i32[5] = [0, 42, -1, 20, 42];
    Sort<i32>(Values, Descending);
    if Values[0] != 42 || Values[1] != 42 || Values[4] != -1 { return 1; }
    switch BinarySearch<i32>(Values, 42, Descending) {
        case .Ok { Value = Index } => { if Index != 0 { return 2; } }
        case .Err { Error = _ } => return 3;
    }
    switch BinarySearch<i32>(Values, 21, Descending) {
        case .Err { Error = Index } => { if Index != 2 { return 4; } }
        case .Ok { Value = _ } => return 5;
    }
    return 0;
}
Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library