Skip to main content
Skip table of contents

sort Statement

Syntax

sort anArray using <expression with element and nextElement>

Semantics

sort allows you to sort an array by using an expression to compare array elements with each other. The sort algorithm will call the expression each time two elements need to be compared, see also the examples below.

It is not determined what sort algorithm will actually be used, though a Quicksort derivative is the most common.

Be aware that sort algorithms are not linear (typically n log n to n2 iterations for n elements). Therefore, you should keep the expression as compact as possible.

When using a boolean expression, make sure that equality always returns true (e.g. by using <= instead of <). Otherwise, the implemented sort algorithm may take longer than necessary or not terminate at all.

Substitutables

anArray

Can be any variable or object attribute having the type Array.

<expression with element and next element>

This expression must use the currently evaluated array element and its next neighbor and indicates if the current order is OK or not.

element and nextElement are keywords and allow you to use relative references (as opposed to normal, absolute indices) to single elements of an array (see Get Array Element Operator []).

Examples

Sort an array of integers in ascending order

NONE
set sorted = sort myList using element - nextElement;

Sort an array of strings in ascending order

NONE
set sorted = sort myList using element.text < nextElement.text;

Sort an array using dates in descending

NONE
set sorted = sort myList using element.date > nextElement.date;

Sort an array using boolean return values

NONE
set sorted = sort myList using element <= nextElement;

Sort an array with complex elements over 2 sort key

NONE
set sorted = sort inputContainer.elements using (element.aString =  nextElement.aString and element.anInteger < nextElement.anInteger) or element.aString < nextElement.aString;

Some Examples

an integer

< 0

element precedes nextElement

= 0

element and nextElement are equal

> 0

element succeeds nextElement

a boolean

true

element precedes nextElement

false

element succeeds nextElement

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.