Qu

sort

Builtin index · Collections & strings

result = sort(list, [descending])

Input: list, descending · Output: result

What it is

list is a List of Str or a numeric Vec; optional descending (bool, default false). A list of strings sorts lexicographically, a numeric vector sorts numerically.

Overloads

list_of_str can be List or Str.

2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = sort(list_of_str, [descending])
list_of_str List or Strlist_of_str is a List in which every element is a Str.
descending bool, optionaloptional descending (bool, default false).
result List
list_of_str is a List in which every element is a Str. Sorts lexicographically. Returns a List.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

y = [5, 2, 9, 1, 1, 9]
print("sort ascending  = {sort(y)}")
print("sort descending = {sort(y, true)}")
print("argsort(y) = {argsort(y)}")   # the permutation that would sort y

output
sort ascending  = [1, 1, 2, 5, 9, 9]
sort descending = [9, 9, 5, 2, 1, 1]
argsort(y) = [3, 4, 1, 0, 2, 5]

from fundamentals

words = ["banana", "apple", "cherry"]
print(sort(words))            # ("apple", "banana", "cherry") -- a List, lexicographic

nums = [3, 1, 4, 1, 5]
print(sort(nums))             # [1, 1, 3, 4, 5] -- a Vec, numeric

print(type(sort(words)))      # "list"

print(type(sort(nums)))       # "vector"

output
("apple", "banana", "cherry")
[1, 1, 3, 4, 5]
list
vector

from collections-strings

xs = [3, 1, 4, 1, 5, 9, 2, 6]
print(unique(xs))          # [1, 2, 3, 4, 5, 6, 9]

print(sort(xs))            # [1, 1, 2, 3, 4, 5, 6, 9]

print(argsort(xs))         # permutation of indices, ascending

print(sort(xs, true))      # descending

output
[1, 2, 3, 4, 5, 6, 9]
[1, 1, 2, 3, 4, 5, 6, 9]
[1, 3, 6, 0, 2, 4, 7, 5]
[9, 6, 5, 4, 3, 2, 1, 1]

from collections-strings

CodeInputOutputDescription
result = sort(vec, [descending])
vec Vecvec is (or coerces to) a numeric Vec.
descending bool, optionaloptional descending (bool, default false).
result Vec
vec is (or coerces to) a numeric Vec. Sorts numerically via f64::total_cmp, immune to a stray NaN. Returns a Vec.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

y = [5, 2, 9, 1, 1, 9]
print("sort ascending  = {sort(y)}")
print("sort descending = {sort(y, true)}")
print("argsort(y) = {argsort(y)}")   # the permutation that would sort y

output
sort ascending  = [1, 1, 2, 5, 9, 9]
sort descending = [9, 9, 5, 2, 1, 1]
argsort(y) = [3, 4, 1, 0, 2, 5]

from fundamentals

words = ["banana", "apple", "cherry"]
print(sort(words))            # ("apple", "banana", "cherry") -- a List, lexicographic

nums = [3, 1, 4, 1, 5]
print(sort(nums))             # [1, 1, 3, 4, 5] -- a Vec, numeric

print(type(sort(words)))      # "list"

print(type(sort(nums)))       # "vector"

output
("apple", "banana", "cherry")
[1, 1, 3, 4, 5]
list
vector

from collections-strings

xs = [3, 1, 4, 1, 5, 9, 2, 6]
print(unique(xs))          # [1, 2, 3, 4, 5, 6, 9]

print(sort(xs))            # [1, 1, 2, 3, 4, 5, 6, 9]

print(argsort(xs))         # permutation of indices, ascending

print(sort(xs, true))      # descending

output
[1, 2, 3, 4, 5, 6, 9]
[1, 1, 2, 3, 4, 5, 6, 9]
[1, 3, 6, 0, 2, 4, 7, 5]
[9, 6, 5, 4, 3, 2, 1, 1]

from collections-strings

How it works

sort(x, [descending]) — ascending by default, matching sort_by(table, col, [descending])'s own positional-bool convention rather than a desc=/order= kwarg. Uses f64::total_cmp rather than partial_cmp().unwrap_or( Equal) — the latter isn't a valid total order (it treats a NaN as "equal" to everything), which doesn't just leave the NaN in an arbitrary spot but silently corrupts the ordering of the *other*, perfectly sortable elements around it too: sort([5, 3, nan, 1, 4, 2]) used to return [3, 5, NaN, 1, 2, 4] (genuinely unsorted), not merely "sorted except for where NaN landed."

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.