Qu

argsort

Builtin index · Collections & strings

result = argsort(x, [descending])

Input: x, descending · Output: result

What it is

x is a numeric Vec; optional descending (bool, default false).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = argsort(x, [descending])
x Vecx is a numeric Vec.
descending bool, optionaloptional descending (bool, default false).
number —0-based indices
Returns a Vec of number (0-based indices): the permutation that would sort x — the index-returning sibling of sort, same convention as argmin/argmax.

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

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

argsort(x, [descending]) — the permutation that would sort x, i.e. the index-returning sibling of sort (same "return an index" convention as argmin/argmax/where).

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