Skip to content

sort

sort

The current repository does not ship a stdlib/sort.tg module. Sorting APIs such as sort, binary_search, lower_bound, and upper_bound are planned, but there is no shipped module to import today.

Current status

ItemStatus
stdlib/sort.tg filenot present
sort(v) helpernot present
binary_search(v, x) helpernot present
lower_bound / upper_bound helpersnot present

Available building blocks

Today, the closest shipped helpers are in std.math and the core language:

Current APISignatureUse
math.minfunc min<T: Ordered>(left: T, right: T) -> Tchoose lower value
math.maxfunc max<T: Ordered>(left: T, right: T) -> Tchoose higher value
math.clampfunc clamp<T: Ordered>(value: T, lo: T, hi: T) -> Tbound comparisons
for item in itemsitems: Array[T]iterate over runtime arrays

Examples

import std.math as math
func sort_two(left: i32, right: i32) -> i32:
return math.min(left, right)
import std.math as math
func cap_score(value: i32) -> i32:
return math.clamp(value, 0, 100)
func is_non_decreasing(items: Array[i32]) -> bool:
let first = true
let prev: i32 = 0
for item in items:
if (first):
prev = item
first = false
else:
if (item < prev):
return false
prev = item
return true