Sort
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 upper case letter coming before lower-case letters in the sort-order. For example, "Zebra" comes before "apple". The optional «caseInsensitive» parameter causes comparisons to occur in a case-insensitive fashion when set to True
:
Sort(a, i, caseInsensitive: True)
Enable comment auto-refresher