Sort

Revision as of 06:47, 14 August 2015 by Karenlee (talk | contribs)

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» reverses the sort order when set to True, 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» parameter causes comparisons to occur in a case-insensitive fashion when set to True:

Sort(A,I,caseInsensitive:true)


Examples

Suppose A is the following table:

J
I 3 2 1
1 3 2
2 1 2
1 2 1
2 1 2

Then:

Sort(A,I) → 
J
I 1 1 1
1 1 1
2 2 2
2 2 2
3 3 2

And:

Sort(A,I,keyIndex:J) →
J
I 1 2 1
1 3 2
2 1 2
2 1 2
3 2 1


Multi-key example

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

See Also

Comments


You are not allowed to post comments.