Skip to content

std/sort

std/sort — free sort_* functions for i32[] and string[]. Insertion-sort implementations: O(n²) worst case, but for the compiler’s small-list use cases (sorted field offsets, declaration orders, capacity hints) it’s fine and avoids the closure-arg infrastructure a generic sort_by[T](arr: T[], cmp: (T, T) => i32) would need. Migrated from the auto-injected prelude per docs/PRELUDE-TO-MODULES.md. The auto-prelude declares import "std/sort"; so existing programs that call sort_i32_asc(arr) etc. by bare name keep working unchanged. string_cmp / string_cmp_ci are the underlying byte- lex comparators the string sorts use; exporting them so callers that need direct ordering checks (without a full sort) don’t have to roll their own.

pub function sort_i32_asc(arr: i32[]): i32[]
pub function sort_i64_asc(arr: i64[]): i64[]
pub function sort_i64_desc(arr: i64[]): i64[]
pub function sort_u32_asc(arr: u32[]): u32[]
pub function sort_u64_asc(arr: u64[]): u64[]
pub function sort_i32_desc(arr: i32[]): i32[]
pub function string_cmp(a: string, b: string): i32

string_cmp(a, b) — lexicographic byte-order comparator. Returns -1 / 0 / 1. Lang’s < / > operators only work on numerics; string-array sort needs a dedicated three-way comparator. Compares byte-by-byte up to the shorter length, then breaks ties on length (shorter < longer).

pub function sort_strings_asc(arr: string[]): string[]

sort_strings_asc(arr) / sort_strings_desc(arr) — lexicographic byte-order sort of a string array. Insertion sort like the i32 helpers; O(n²) worst case fine for the small-list use cases (config keys, sorted output for stable tests, alphabetised CLI flags).

pub function sort_strings_desc(arr: string[]): string[]
pub function string_cmp_ci(a: string, b: string): i32

string_cmp_ci(a, b) — same as string_cmp but ASCII case- folded per byte. Useful as the ordering for case-insensitive sort.

pub function sort_strings_asc_ci(arr: string[]): string[]

sort_strings_asc_ci(arr) — case-insensitive ascending sort. Useful for user-facing alphabetical listings where case shouldn’t fragment the ordering.

pub function sort_i32_inplace_asc(arr: i32[]): i32[]

sort_i32_inplace_asc(own arr) — ascending insertion-sort performed directly on the (owned) input buffer; returns that same buffer, now sorted.

pub function sort_i32_inplace_desc(arr: i32[]): i32[]

sort_i32_inplace_desc(own arr) — as above, descending.