std/sort
std/sort
Section titled “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.
sort_i32_asc
Section titled “sort_i32_asc”pub function sort_i32_asc(arr: i32[]): i32[]sort_i64_asc
Section titled “sort_i64_asc”pub function sort_i64_asc(arr: i64[]): i64[]sort_i64_desc
Section titled “sort_i64_desc”pub function sort_i64_desc(arr: i64[]): i64[]sort_u32_asc
Section titled “sort_u32_asc”pub function sort_u32_asc(arr: u32[]): u32[]sort_u64_asc
Section titled “sort_u64_asc”pub function sort_u64_asc(arr: u64[]): u64[]sort_i32_desc
Section titled “sort_i32_desc”pub function sort_i32_desc(arr: i32[]): i32[]string_cmp
Section titled “string_cmp”pub function string_cmp(a: string, b: string): i32string_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).
sort_strings_asc
Section titled “sort_strings_asc”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).
sort_strings_desc
Section titled “sort_strings_desc”pub function sort_strings_desc(arr: string[]): string[]string_cmp_ci
Section titled “string_cmp_ci”pub function string_cmp_ci(a: string, b: string): i32string_cmp_ci(a, b) — same as string_cmp but ASCII case-
folded per byte. Useful as the ordering for case-insensitive
sort.
sort_strings_asc_ci
Section titled “sort_strings_asc_ci”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.
sort_i32_inplace_asc
Section titled “sort_i32_inplace_asc”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.
sort_i32_inplace_desc
Section titled “sort_i32_inplace_desc”pub function sort_i32_inplace_desc(arr: i32[]): i32[]sort_i32_inplace_desc(own arr) — as above, descending.