Sort
New to Analytica 4.2
Sort(A,I)
Sorts an 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.
Multi-key sorting
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.
Descending sort order
The optional parameter descending:true reverses the sort order, e.g.:
- Sort(A,I,descending:true)
Case Sensitivity
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:true parameter causes comparisons to occur in a case-insensitive fashion:
- Sort(A,I,caseInsensitive:true)
Library
Array functions
Examples
Suppose
A → |
|
---|
Sort(A,I) → |
|
---|
Sort(A,I,keyIndex:J) → |
|
---|
See Also
User Guide
Sort(x,i) returns the elements of x, reordered along index i in sorted order. The equivalent can be accomplished using the SortIndex function as:
x[i=SortIndex(x,i)]
To perform a multi-key sort, in which the first key determines the sort order unless there are ties, in which the second key breaks the ties, the third key breaks any remaining ties, etc., collect the key criteria along an index K and specify the optional keyIndex parameter, e.g.:
Sort(Array(K,[key1,key2,key3]),i,keyIndex:K)
The data is sorted in ascending order (from smallest to largest), unless you specify the optional parameter descending:true, which then reorders from largest to smallest. The optional parameter caseInsensitive:true ignores lower/upper case in textual comparisons. Either of these may also be indexed by the keyIndex when the order or case-insensitivity varies by key.
Multi-key example:
Sort(NumMaintEvents, CarNum, KeyIndex: MaintType, descending:true)→
MaintType v, CarNum >
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 |
Enable comment auto-refresher