package core:sort
Overview
A sorting interface and algorithms.
Index
Types (1)
Constants (0)
This section is empty.
Variables (0)
This section is empty.
Procedures (30)
Procedure Groups (0)
This section is empty.
Types
Interface ¶
Interface :: struct { len: proc(it: Interface) -> int, less: proc(it: Interface, i, j: int) -> bool, swap: proc(it: Interface, i, j: int), collection: rawptr, }
A generic interface describing a sequence of elements that can be sorted.
It provides the operations the sorting procedures use to inspect and reorder
the elements: len reports the number of elements, less compares two
elements, and swap exchanges two elements. The underlying data lives in
collection.
Use slice_interface to obtain an Interface for a slice, or
reverse_interface to wrap an Interface so that it is ordered in reverse.
Related Procedures With Parameters
Constants
This section is empty.
Variables
This section is empty.
Procedures
ORD ¶
ORD :: intrinsics.type_is_ordered
ORD :: proc($T: typeid) -> bool {…}
Returns true if the type is an integer, float, rune, any string, pointer, or multi-pointer
bubble_sort ¶
bubble_sort :: proc(array: $A/[]$T) {…}
Sorts a slice of ordered elements in place using bubble sort, in ascending order.
Inputs:
- array The slice to sort.
Example:
import "core:fmt"
import "core:sort"
bubble_sort_example :: proc() {
data := []int{5, 3, 8, 1}
sort.bubble_sort(data)
fmt.println(data)
}
[1, 3, 5, 8]
bubble_sort_proc ¶
bubble_sort_proc :: proc(array: $A/[]$T, f: proc($T, $T) -> int) {…}
Sorts a slice in place using bubble sort, ordered by the given comparator.
The comparator f is called with two elements and must return a negative
number if the first is less than the second, 0 if they are equal, and a
positive number otherwise.
Inputs:
- array The slice to sort.
- f The comparator used to order the elements.
Example:
import "core:fmt"
import "core:sort"
bubble_sort_proc_example :: proc() {
data := []int{5, 3, 8, 1}
sort.bubble_sort_proc(data, sort.compare_ints)
fmt.println(data)
}
[1, 3, 5, 8]
compare_bools ¶
Compares two booleans for ordering, where false is considered less than
true.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_bools_example :: proc() {
fmt.println(sort.compare_bools(false, true))
fmt.println(sort.compare_bools(true, true))
fmt.println(sort.compare_bools(true, false))
}
-1 0 1
compare_f32s ¶
Compares two f32 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_f32s_example :: proc() {
fmt.println(sort.compare_f32s(1.0, 2.0))
fmt.println(sort.compare_f32s(2.0, 2.0))
fmt.println(sort.compare_f32s(3.0, 2.0))
}
-1 0 1
compare_f64s ¶
Compares two f64 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_f64s_example :: proc() {
fmt.println(sort.compare_f64s(1.0, 2.0))
fmt.println(sort.compare_f64s(2.0, 2.0))
fmt.println(sort.compare_f64s(3.0, 2.0))
}
-1 0 1
compare_i16s ¶
Compares two i16 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_i16s_example :: proc() {
fmt.println(sort.compare_i16s(-1, 2))
fmt.println(sort.compare_i16s(2, 2))
fmt.println(sort.compare_i16s(3, 2))
}
-1 0 1
compare_i32s ¶
Compares two i32 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_i32s_example :: proc() {
fmt.println(sort.compare_i32s(-1, 2))
fmt.println(sort.compare_i32s(2, 2))
fmt.println(sort.compare_i32s(3, 2))
}
-1 0 1
compare_i64s ¶
Compares two i64 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_i64s_example :: proc() {
fmt.println(sort.compare_i64s(-1, 2))
fmt.println(sort.compare_i64s(2, 2))
fmt.println(sort.compare_i64s(3, 2))
}
-1 0 1
compare_i8s ¶
Compares two i8 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_i8s_example :: proc() {
fmt.println(sort.compare_i8s(-1, 2))
fmt.println(sort.compare_i8s(2, 2))
fmt.println(sort.compare_i8s(3, 2))
}
-1 0 1
compare_ints ¶
Compares two int values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_ints_example :: proc() {
fmt.println(sort.compare_ints(1, 2))
fmt.println(sort.compare_ints(2, 2))
fmt.println(sort.compare_ints(3, 2))
}
-1 0 1
compare_strings ¶
Compares two strings lexicographically (byte-wise) for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_strings_example :: proc() {
fmt.println(sort.compare_strings("apple", "banana"))
fmt.println(sort.compare_strings("apple", "apple"))
fmt.println(sort.compare_strings("banana", "apple"))
}
-1 0 1
compare_u16s ¶
Compares two u16 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_u16s_example :: proc() {
fmt.println(sort.compare_u16s(1, 2))
fmt.println(sort.compare_u16s(2, 2))
fmt.println(sort.compare_u16s(3, 2))
}
-1 0 1
compare_u32s ¶
Compares two u32 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_u32s_example :: proc() {
fmt.println(sort.compare_u32s(1, 2))
fmt.println(sort.compare_u32s(2, 2))
fmt.println(sort.compare_u32s(3, 2))
}
-1 0 1
compare_u64s ¶
Compares two u64 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_u64s_example :: proc() {
fmt.println(sort.compare_u64s(1, 2))
fmt.println(sort.compare_u64s(2, 2))
fmt.println(sort.compare_u64s(3, 2))
}
-1 0 1
compare_u8s ¶
Compares two u8 values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_u8s_example :: proc() {
fmt.println(sort.compare_u8s(1, 2))
fmt.println(sort.compare_u8s(2, 2))
fmt.println(sort.compare_u8s(3, 2))
}
-1 0 1
compare_uints ¶
Compares two uint values for ordering.
Returns:
- A negative number if
ais less thanb,0if they are equal, and a positive number otherwise.
Example:
import "core:fmt"
import "core:sort"
compare_uints_example :: proc() {
fmt.println(sort.compare_uints(1, 2))
fmt.println(sort.compare_uints(2, 2))
fmt.println(sort.compare_uints(3, 2))
}
-1 0 1
heap_sort ¶
heap_sort :: proc(array: $A/[]$T) {…}
Sorts a slice of ordered elements in place using heap sort, in ascending order.
Inputs:
- array The slice to sort.
Example:
import "core:fmt"
import "core:sort"
heap_sort_example :: proc() {
data := []int{5, 3, 8, 1}
sort.heap_sort(data)
fmt.println(data)
}
[1, 3, 5, 8]
heap_sort_proc ¶
heap_sort_proc :: proc(array: $A/[]$T, f: proc($T, $T) -> int) {…}
Sorts a slice in place using heap sort, ordered by the given comparator.
The comparator f is called with two elements and must return a negative
number if the first is less than the second, 0 if they are equal, and a
positive number otherwise.
Inputs:
- array The slice to sort.
- f The comparator used to order the elements.
Example:
import "core:fmt"
import "core:sort"
heap_sort_proc_example :: proc() {
data := []int{5, 3, 8, 1}
sort.heap_sort_proc(data, sort.compare_ints)
fmt.println(data)
}
[1, 3, 5, 8]
is_sorted ¶
Reports whether the elements of an Interface are in ascending order.
Inputs:
- it The
Interfaceto check.
Returns:
trueif the elements are sorted in ascending order,falseotherwise.
Example:
import "core:fmt"
import "core:sort"
is_sorted_example :: proc() {
a := []int{1, 2, 3}
b := []int{3, 1, 2}
fmt.println(sort.is_sorted(sort.slice_interface(&a)))
fmt.println(sort.is_sorted(sort.slice_interface(&b)))
}
true false
merge_sort ¶
merge_sort :: proc(array: $A/[]$T) {…}
Sorts a slice of ordered elements in place using merge sort, in ascending order.
Inputs:
- array The slice to sort.
Example:
import "core:fmt"
import "core:sort"
merge_sort_example :: proc() {
data := []int{5, 3, 8, 1}
sort.merge_sort(data)
fmt.println(data)
}
[1, 3, 5, 8]
merge_sort_proc ¶
merge_sort_proc :: proc(array: $A/[]$T, f: proc($T, $T) -> int) {…}
Sorts a slice in place using merge sort, ordered by the given comparator.
The comparator f is called with two elements and must return a negative
number if the first is less than the second, 0 if they are equal, and a
positive number otherwise.
Inputs:
- array The slice to sort.
- f The comparator used to order the elements.
Example:
import "core:fmt"
import "core:sort"
merge_sort_proc_example :: proc() {
data := []int{5, 3, 8, 1}
sort.merge_sort_proc(data, sort.compare_ints)
fmt.println(data)
}
[1, 3, 5, 8]
quick_sort ¶
quick_sort :: proc(array: $A/[]$T) {…}
Sorts a slice of ordered elements in place using quick sort, in ascending order.
Inputs:
- array The slice to sort.
Example:
import "core:fmt"
import "core:sort"
quick_sort_example :: proc() {
data := []int{5, 3, 8, 1}
sort.quick_sort(data)
fmt.println(data)
}
[1, 3, 5, 8]
quick_sort_proc ¶
quick_sort_proc :: proc(array: $A/[]$T, f: proc($T, $T) -> int) {…}
Sorts a slice in place using quick sort, ordered by the given comparator.
The comparator f is called with two elements and must return a negative
number if the first is less than the second, 0 if they are equal, and a
positive number otherwise.
Inputs:
- array The slice to sort.
- f The comparator used to order the elements.
Example:
import "core:fmt"
import "core:sort"
quick_sort_proc_example :: proc() {
data := []int{5, 3, 8, 1}
sort.quick_sort_proc(data, sort.compare_ints)
fmt.println(data)
}
[1, 3, 5, 8]
reverse_interface ¶
Wraps an Interface so that the ordering is reversed, making the sorting
procedures sort the elements in descending order.
Inputs:
- it A pointer to the
Interfaceto reverse.
Returns:
- A new
Interfacethat reverses the ordering ofit.
Example:
import "core:fmt"
import "core:sort"
reverse_interface_example :: proc() {
data := []int{5, 2, 8}
s := sort.slice_interface(&data)
sort.sort(sort.reverse_interface(&s))
fmt.println(data)
}
[8, 5, 2]
reverse_sort ¶
reverse_sort :: proc(it: Interface) {…}
Sorts the elements of an Interface in descending order, in place.
This sort is not guaranteed to be stable.
Inputs:
- it The
Interfaceto sort.
Example:
import "core:fmt"
import "core:sort"
reverse_sort_example :: proc() {
data := []int{5, 2, 8, 1}
sort.reverse_sort(sort.slice_interface(&data))
fmt.println(data)
}
[8, 5, 2, 1]
rotate ¶
Rotates the elements of an Interface in the range [a, b) so that the
element at index m becomes the first element of the range.
Inputs:
- it The
Interface. - a Start index of the range.
- m The index whose element becomes first after the rotation.
- b Exclusive end index of the range.
Example:
import "core:fmt"
import "core:sort"
rotate_example :: proc() {
data := []int{0, 1, 2, 3}
sort.rotate(sort.slice_interface(&data), 0, 2, 4)
fmt.println(data)
}
[2, 3, 0, 1]
slice_interface ¶
slice_interface :: proc(s: ^$T/[]$E) -> Interface {…}
Creates an Interface over the given slice of ordered elements, for use with
the sorting procedures.
The elements are compared with the < operator, so the resulting sort is in
ascending order.
Inputs:
- s A pointer to the slice to wrap.
Returns:
- An
Interfacethat describes the elements ofs.
Example:
import "core:fmt"
import "core:sort"
slice_interface_example :: proc() {
data := []int{3, 1, 2}
it := sort.slice_interface(&data)
fmt.println(sort.is_sorted(it))
sort.sort(it)
fmt.println(data)
}
false [1, 2, 3]
sort ¶
sort :: proc(it: Interface) {…}
Sorts the elements of an Interface in place.
This sort is not guaranteed to be stable.
Inputs:
- it The
Interfaceto sort.
Example:
import "core:fmt"
import "core:sort"
sort_example :: proc() {
data := []int{5, 2, 8, 1, 9}
sort.sort(sort.slice_interface(&data))
fmt.println(data)
}
[1, 2, 5, 8, 9]
swap_range ¶
Swaps n elements beginning at index a with the n elements beginning at
index b.
Inputs:
- it The
Interface. - a Start index of the first range to swap.
- b Start index of the second range to swap.
- n The number of elements to swap.
Example:
import "core:fmt"
import "core:sort"
swap_range_example :: proc() {
data := []int{1, 2, 3, 4}
sort.swap_range(sort.slice_interface(&data), 0, 2, 2)
fmt.println(data)
}
[3, 4, 1, 2]
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-10 (vendor "odin") Windows_amd64 @ 2026-10-10 00:25:51.511820200 +0000 UTC