#sort
Xu 0.16.0 API
import: #sort
counts: 17 functions, 0 methods, 1 types, 0 constants, 0 method sets
ints/f64s/strings introsort; slice / slice_func by Ord or callback.
slice
pub fn slice#T:Ord(a $&[]T)
Purpose
Sort the slice in ascending order in place for subsequent searching or iteration.
Parameters
a $&[]T- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
Usage notes
- Built-in
int, string and f64 work directly; a custom element type must provide a less(other T) bool method.
ints
Purpose
Sort the slice in ascending order in place for subsequent searching or iteration.
Parameters
a $&[]int- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
strings
pub fn strings(a $&[]string)
Purpose
Sort the slice in ascending order in place for subsequent searching or iteration.
Parameters
a $&[]string- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
f64s
Purpose
Sort the slice in ascending order in place for subsequent searching or iteration.
Parameters
a $&[]f64- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
slice_func
pub fn slice_func#T(a $&[]T, less fn(T, T) bool)
Purpose
Sort the slice in ascending order in place for subsequent searching or iteration.
Parameters
a $&[]T- Slice to process; sorting calls borrow it exclusively and modify it in place.
less fn(T, T) bool- Return
true when the first element should precede the second. Equal elements must compare false, and the ordering must be consistent.
Returns
No return value; the sorted elements remain in a.
struct Search
pub struct Search {
pub index int
pub found bool
}
Purpose
Binary search helper encapsulating position and compare context.
Public fields
index int- field (int)
found bool- field (bool)
lookup_ints
pub fn lookup_ints(a &[]int, v int) Search
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]int- Already ascending-sorted slice; this call does not sort it first.
v int- Value to locate.
Returns
Search.index is the first position not less than v, in 0..a.len; found indicates an exact match.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
lookup_strings
pub fn lookup_strings(a &[]string, v string) Search
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]string- Already ascending-sorted slice; this call does not sort it first.
v string- Value to locate.
Returns
Search.index is the first position not less than v, in 0..a.len; found indicates an exact match.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
lookup_f64s
pub fn lookup_f64s(a &[]f64, v f64) Search
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]f64- Already ascending-sorted slice; this call does not sort it first.
v f64- Value to locate.
Returns
Search.index is the first position not less than v, in 0..a.len; found indicates an exact match.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
search_ints
pub fn search_ints(a &[]int, v int) int
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]int- Already ascending-sorted slice; this call does not sort it first.
v int- Value to locate.
Returns
First position not less than v, in 0..a.len. A missing value yields an insertion position, not -1.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
search_strings
pub fn search_strings(a &[]string, v string) int
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]string- Already ascending-sorted slice; this call does not sort it first.
v string- Value to locate.
Returns
First position not less than v, in 0..a.len. A missing value yields an insertion position, not -1.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
search_f64s
pub fn search_f64s(a &[]f64, v f64) int
Purpose
Find the lower bound in a sorted slice, either to locate a value or to determine its ordered insertion position.
Parameters
a &[]f64- Already ascending-sorted slice; this call does not sort it first.
v f64- Value to locate.
Returns
First position not less than v, in 0..a.len. A missing value yields an insertion position, not -1.
Usage notes
- An empty slice yields position
0; duplicates yield the first position. Check the value or use lookup_*.found to distinguish a match from an insertion point.
is_sorted_ints
pub fn is_sorted_ints(a &[]int) bool
Purpose
Check ascending order, allowing equal values, without modifying the slice.
Parameters
a &[]int- Read-only slice to inspect.
Returns
true when sorted; also true for empty and single-element slices.
is_sorted_strings
pub fn is_sorted_strings(a &[]string) bool
Purpose
Check ascending order, allowing equal values, without modifying the slice.
Parameters
a &[]string- Read-only slice to inspect.
Returns
true when sorted; also true for empty and single-element slices.
is_sorted_f64s
pub fn is_sorted_f64s(a &[]f64) bool
Purpose
Check ascending order, allowing equal values, without modifying the slice.
Parameters
a &[]f64- Read-only slice to inspect.
Returns
true when sorted; also true for empty and single-element slices.
reverse_ints
pub fn reverse_ints(a $&[]int)
Purpose
Sort the entire slice in descending order in place by sorting ascending and then reversing.
Parameters
a $&[]int- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
Usage notes
- Use
slices.reverse_* to reverse the existing sequence without sorting it.
Call example
Descending sort versus sequence reversal
@main
#sort
#slices
fn main() {
$ordered := [2, 1, 3]
sort.reverse_ints($&ordered)
for value in ordered { println(value) }
$flipped := [2, 1, 3]
slices.reverse_ints($&flipped)
for value in flipped { println(value) }
}
Expected output:
3
2
1
3
1
2
reverse_strings
pub fn reverse_strings(a $&[]string)
Purpose
Sort the entire slice in descending order in place by sorting ascending and then reversing.
Parameters
a $&[]string- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
Usage notes
- Use
slices.reverse_* to reverse the existing sequence without sorting it.
reverse_f64s
pub fn reverse_f64s(a $&[]f64)
Purpose
Sort the entire slice in descending order in place by sorting ascending and then reversing.
Parameters
a $&[]f64- Slice to process; sorting calls borrow it exclusively and modify it in place.
Returns
No return value; the sorted elements remain in a.
Usage notes
- Use
slices.reverse_* to reverse the existing sequence without sorting it.