package core:sort

⌘K
Ctrl+K
or
/

    Overview

    A sorting interface and algorithms.

    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)
    }
    
    Output:
    [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)
    }
    
    Output:
    [1, 3, 5, 8]
    

    compare_bools ¶

    compare_bools :: proc(a, b: bool) -> int {…}
     

    Compares two booleans for ordering, where false is considered less than true.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_f32s ¶

    compare_f32s :: proc(a, b: f32) -> int {…}
     

    Compares two f32 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_f64s ¶

    compare_f64s :: proc(a, b: f64) -> int {…}
     

    Compares two f64 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_i16s ¶

    compare_i16s :: proc(a, b: i16) -> int {…}
     

    Compares two i16 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_i32s ¶

    compare_i32s :: proc(a, b: i32) -> int {…}
     

    Compares two i32 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_i64s ¶

    compare_i64s :: proc(a, b: i64) -> int {…}
     

    Compares two i64 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_i8s ¶

    compare_i8s :: proc(a, b: i8) -> int {…}
     

    Compares two i8 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_ints ¶

    compare_ints :: proc(a, b: int) -> int {…}
     

    Compares two int values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_strings ¶

    compare_strings :: proc(a, b: string) -> int {…}
     

    Compares two strings lexicographically (byte-wise) for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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"))
    }
    
    Output:
    -1
    0
    1
    

    compare_u16s ¶

    compare_u16s :: proc(a, b: u16) -> int {…}
     

    Compares two u16 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_u32s ¶

    compare_u32s :: proc(a, b: u32) -> int {…}
     

    Compares two u32 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_u64s ¶

    compare_u64s :: proc(a, b: u64) -> int {…}
     

    Compares two u64 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_u8s ¶

    compare_u8s :: proc(a, b: u8) -> int {…}
     

    Compares two u8 values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -1
    0
    1
    

    compare_uints ¶

    compare_uints :: proc(a, b: uint) -> int {…}
     

    Compares two uint values for ordering.

    Returns:

    • A negative number if a is less than b, 0 if 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))
    }
    
    Output:
    -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)
    }
    
    Output:
    [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)
    }
    
    Output:
    [1, 3, 5, 8]
    

    is_sorted ¶

    is_sorted :: proc(it: Interface) -> bool {…}
     

    Reports whether the elements of an Interface are in ascending order.

    Inputs:

    Returns:

    • true if the elements are sorted in ascending order, false otherwise.
    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)))
    }
    
    Output:
    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)
    }
    
    Output:
    [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)
    }
    
    Output:
    [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)
    }
    
    Output:
    [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)
    }
    
    Output:
    [1, 3, 5, 8]
    

    reverse_interface ¶

    reverse_interface :: proc(it: ^Interface) -> Interface {…}
     

    Wraps an Interface so that the ordering is reversed, making the sorting procedures sort the elements in descending order.

    Inputs:

    Returns:

    • A new Interface that reverses the ordering of it.
    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)
    }
    
    Output:
    [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:

    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)
    }
    
    Output:
    [8, 5, 2, 1]
    

    rotate ¶

    rotate :: proc(it: Interface, a, m, b: int) {…}
     

    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)
    }
    
    Output:
    [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 Interface that describes the elements of s.
    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)
    }
    
    Output:
    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:

    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)
    }
    
    Output:
    [1, 2, 5, 8, 9]
    

    swap_range ¶

    swap_range :: proc(it: Interface, a, b, n: int) {…}
     

    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)
    }
    
    Output:
    [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