Grav v1.14.0

Sort

import "std/core" (included in import "std") · namespace std.core

Sorting and binary search for Arrays. Sort.sort sorts arrays of numbers and strings in their natural order; Sort.sortBy sorts anything with a comparison function. All sorts are stable (equal elements keep their order) and use TimSort, which is especially fast on data that is already partly sorted.

A comparison function cmp(a, b) returns a negative number when a comes first, a positive number when b comes first, and 0 when they are equal.

grav
import "std"

class Player {
    public name: string
    public score: int
    constructor(name: string, score: int) { self.name = name; self.score = score }
}

fn main() {
    let nums: Array<int> = [5, 3, 9, 1]
    Sort.sort(nums)
    print(nums)
    print(Sort.binarySearch(nums, 9))
    print(Sort.binarySearch(nums, 4))      // not found: -(insertion point) - 1

    let names: Array<string> = ["pear", "fig", "apple"]
    Sort.sortDescending(names)
    print(names)

    let players = new Array<Player>()
    players.push(new Player("ann", 30))
    players.push(new Player("bob", 50))
    players.push(new Player("cy", 30))
    Sort.sortBy(players, fn(a, b) => b.score - a.score)   // highest first
    for (p in players) { print("${p.name} ${p.score}") }
}
Output
[1, 3, 5, 9]
3
-3
["pear", "fig", "apple"]
bob 50
ann 30
cy 30

Array.sortBy(cmp) does the same as Sort.sortBy(array, cmp). A comparison that is not consistent (it says a < b and b < a) can make the sort throw a ValueError.

API

class Sort

Static members

  • static fn sort(xs: Array<int>) — Sorts in place, smallest first.
  • static fn sortDescending(xs: Array<int>) — Sorts in place, largest first.
  • static fn isSorted(xs: Array<int>) -> bool — Whether the elements are in ascending order.
  • static fn binarySearch(xs: Array<int>, key: int) -> int — In a sorted array: the index of key, or -(insertion point) - 1.
  • static fn sort(xs: Array<long>) — Sorts in place, smallest first.
  • static fn sortDescending(xs: Array<long>) — Sorts in place, largest first.
  • static fn isSorted(xs: Array<long>) -> bool — Whether the elements are in ascending order.
  • static fn binarySearch(xs: Array<long>, key: long) -> int — In a sorted array: the index of key, or -(insertion point) - 1.
  • static fn sort(xs: Array<float>) — Sorts in place, smallest first.
  • static fn sortDescending(xs: Array<float>) — Sorts in place, largest first.
  • static fn isSorted(xs: Array<float>) -> bool — Whether the elements are in ascending order.
  • static fn binarySearch(xs: Array<float>, key: float) -> int — In a sorted array: the index of key, or -(insertion point) - 1.
  • static fn sort(xs: Array<string>) — Sorts in place, smallest first (strings byte by byte).
  • static fn sortDescending(xs: Array<string>) — Sorts in place, largest first.
  • static fn isSorted(xs: Array<string>) -> bool — Whether the elements are in ascending order.
  • static fn binarySearch(xs: Array<string>, key: string) -> int — In a sorted array: the index of key, or -(insertion point) - 1.
  • static fn sortBy<T>(xs: Array<T>, cmp: fn(T, T) -> int) — Sorts in place (stable): cmp(a, b) < 0 puts a first.
  • static fn isSortedBy<T>(xs: Array<T>, cmp: fn(T, T) -> int) -> bool — Whether the elements are in order by cmp (no element sorts before the one ahead of it).
  • static fn binarySearchBy<T>(xs: Array<T>, key: T, cmp: fn(T, T) -> int) -> int — In an array sorted by cmp: the index of an element equal to key, or -(where it would go) - 1.

Edit this page on GitHub