Module std::sort

Sorting utilities for vectors.

Provides type-specific sort and reverse functions. Each returns a new vector โ€” the original is never modified. Integer and string sorting use iterative merge passes with O(n log n) comparisons.

Examples

import std.sort;

fn main() {
    var nums: Vec<i64> = Vec.new();
    nums.push(3);
    nums.push(1);
    nums.push(2);
    let sorted = sort.sort_ints(nums);
    // sorted is [1, 2, 3]
}

Contents

Functions

Function sort_by

pub fn sort_by(v: Vec<T>, less: fn(T, T) -> bool) -> Vec<T>

Sort any Vec<T> in the order less defines, where less(a, b) returns true when a belongs strictly before b. Stable: elements for which neither is less than the other keep their relative order.

Returns a new sorted vector; the original is unchanged.

There is no generic sort() alongside this: it would need an ordering trait bound (T: Ord or similar), and std.builtins does not declare one yet. sort_ints/sort_strings/sort_floats remain the built-in orderings for those concrete types.

There is also no v.sort_by(less) fluent method form: the checker reserves inherent impl Vec<T> { ... } blocks for std/builtins.hew (E_HIR: impl-block shape not yet lowered) and, even there, Vec's method dispatch only forwards runtime-backed (#[extern_symbol]) methods, panicking on a pure-Hew one (hew-types/src/check/methods.rs, "Vec method signature was present immediately before dispatch"). Both are compiler gaps outside this module's reach; see the stdlib-packages lane report for hew-lang/hew#2933.

Examples

import std.sort;

fn main() {
    var nums: Vec<i64> = Vec.new();
    nums.push(3);
    nums.push(1);
    nums.push(2);
    let sorted = sort.sort_by(nums, |a: i64, b: i64| a < b);
    println(sorted[0]);  // 1
    println(sorted[1]);  // 2
    println(sorted[2]);  // 3
}

Function sort_ints

pub fn sort_ints(v: Vec<i64>) -> Vec<i64>

Sort a vector of integers in ascending order.

Returns a new sorted vector; the original is unchanged.

Function sort_ints_counted

pub fn sort_ints_counted(v: Vec<i64>) -> (Vec<i64>, i64)

Sort integers and return the production merge core's comparison count.

This diagnostic form exists for deterministic complexity regression tests.

Function sort_strings

pub fn sort_strings(v: Vec<string>) -> Vec<string>

Sort a vector of strings in alphabetical order.

Returns a new sorted vector; the original is unchanged.

Function sort_strings_counted

pub fn sort_strings_counted(v: Vec<string>) -> (Vec<string>, i64)

Sort strings and return the production merge core's comparison count.

This diagnostic form exists for deterministic complexity regression tests.

Function sort_floats

pub fn sort_floats(v: Vec<f64>) -> Vec<f64>

Sort a vector of floats in ascending order.

Returns a new sorted vector; the original is unchanged.

Function reverse_ints

pub fn reverse_ints(v: Vec<i64>) -> Vec<i64>

Reverse a vector of integers.

Returns a new reversed vector; the original is unchanged.

Function reverse_strings

pub fn reverse_strings(v: Vec<string>) -> Vec<string>

Reverse a vector of strings.

Returns a new reversed vector; the original is unchanged.

Function reverse_floats

pub fn reverse_floats(v: Vec<f64>) -> Vec<f64>

Reverse a vector of floats.

Returns a new reversed vector; the original is unchanged.