Skip to content
ffredyk edited this page Jul 23, 2026 · 1 revision

sort

Category

Array

Arity

Binary<array> sort <ascending>.

<array> sort <boolean>   // true = ascending, false = descending

Description

Sorts the array in-place. The right operand is a Boolean: true for ascending order, false for descending order. nil values always sort to the end regardless of direction. Returns nil.

How It Works

Calls List<Value>.Sort() with a custom comparer. The comparer handles nil values by placing them at the end. For numbers and strings, uses natural ordering. Sorting is unstable — equal elements may change relative order.

Usage

Ascending sort

_arr = [3, 1, 4, 1, 5];
_arr sort true;                // [1, 1, 3, 4, 5]

Descending sort

_scores = [10, 45, 22, 98];
_scores sort false;            // [98, 45, 22, 10]

Mixed types

_arr = [5, nil, 2, nil, 9];
_arr sort true;                // [2, 5, 9, nil, nil]  — nils to end
_arr sort false;               // [9, 5, 2, nil, nil]

String sorting

_names = ["Charlie", "Alice", "Bob"];
_names sort true;              // ["Alice", "Bob", "Charlie"]

Sorting after fill

_scores = [];
{ _scores pushBack (random 100) } forEach [1,2,3,4,5];
_scores sort false;            // sort best-first
_top3 = [_scores select 0, _scores select 1, _scores select 2];

Thread Safety

Not thread-safe. Array must belong to the current scheduler.

See Also

  • reverse — reverse array order
  • min — smaller of two
  • max — larger of two
  • select — element access

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally