Sort


Release:

 • 4.6 •  5.0 •  5.1 •  5.2 •  5.3 •  5.4 •  6.0 •  6.1 •  6.2 •  6.3 •  6.4 •  6.5 •   •  6.6 •  7.0 •  7.1 •  7.2

Sort(a, i)

Sorts array «a» in ascending order along index «i». The result has the same indexes as the original array, but the elements are re-ordered.

Comparison to SortIndex

Sort is equivalent to:

A[I = SortIndex(A, I)]

In several respects, SortIndex is more general than Sort, but in some cases Sort is more convenient. SortIndex provides you with the sorted-order, which can then be used to re-index more than one array. Also, if you are sorting a multi-dimensional array, re-ordering all columns based on the sort-order of one particular "key" column, then you will need to use SortIndex. When SortIndex(a, i) is applied to a multi-dimensional array, every column is sorted independently.

Examples

Let:

Variable A:=
J ▶
I X Y Z
A 3 2 1
B 1 3 2
C 2 1 2
D 1 2 1
E 2 1 2

Then:

Sort(A, I) →
J ▶
I X Y Z
A 1 1 1
B 1 1 1
C 2 2 2
D 2 2 2
E 3 3 2
Sort(A, I, keyIndex: J) →
J ▶
I X Y Z
A 1 2 1
B 1 3 2
C 2 1 2
D 2 1 2
E 3 2 1

Optional Parameters

KeyIndex

Like SortIndex, Sort can be used to perform multi-key sorting. With Sort, this is most convenient when the key columns are ordered from left-to-right along the «keyIndex». With a multi-key sort, the first column of the array (along the «keyIndex») determines the sort order, unless there are ties, in which case the second column breaks the tie. If there is still a tie, then the third column breaks the tie, and so forth. The syntax for using a multi-key sort is:

Sort(a, i, keyIndex)

Note that this syntax provides a convenient way to sort a 2-D array, using the first column to determine the sort order for remaining columns.

The following example is based off of the parameters in the Array Function Example Variables.

Sort(NumMaintEvents, CarNum, KeyIndex: MaintType, descending: true) →
CarNum ▶
MaintType 1 2 3 4 5 6 7
Repair 10 9 4 4 4 4 1
Scheduled 0 0 5 2 2 1 0
Tires 0 0 0 2 1 0 0

Descending

The optional parameter «descending» reverses the sort order when set to True, e.g.:

Sort(a, i, descending: True)

CaseInsensitive

Sort compares text values in a case-sensitive fashion. With the default collation order, case is only a tiebreaker: text is ordered by its letters first, and case decides only between texts that are otherwise identical, with lowercase first. So "apple" comes before "Apple", and both come before "Zebra". (Capital letters precede all lowercase letters only when TextLocale is set to ANSI.) The optional «caseInsensitive» parameter removes that final tiebreaker, so that "apple" and "Apple" tie and keep their original order:

Sort(a, i, caseInsensitive: True)

See Collation Order#Case sensitivity for details.

LessThan

(New to Analytica 7.2) The optional «lessThan» parameter sorts by an ordering of your own. Pass a function of two items that returns true when the first should come before the second: a local function written in place, or the name of a User-Defined Function. For example, to sort an array of Struct instances by their «age» member:

Sort(People, I, lessThan: (Function(x, y) ::= x->age < y->age))

descending: true reverses the order the function defines, and ties keep their original order. See Using an ordering function when sorting for the forms the function can take, how it combines with «keyIndex», and the treatment of Null.

History

See Also

Comments
Loading comments...