Sort
(Engine-Level Function)
Description: | Allows the sorting of an array subsection according to the order of another array. |
Returns: | Nothing |
Usage: | Script Only. |
Function Groups: | Array |
Related to: | PlotXY | TextSearch | SortArray |
Format: | Sort(KeyArrayElem, SortArrayElem, N, Descending [, TypeText, CaseInsensitive, Locale, CmpFlags]) |
Parameters: |
KeyArrayElem | ||||||||||||
Required. An element of the array to be used as the reference for the sort. This array is copied into temporary memory space. The copy is then arranged in order along with the SortArray, but the original copy of the key array is left unchanged after the sort unless it is the same array as SortArray. If the key array contains text strings, the TypeText parameter should be set to true (non-0) to enable alphabetical ordering, otherwise each element will be converted to a number before use as a sort key. |
||||||||||||
SortArrayElem | ||||||||||||
Required. Any array element giving the starting point in the array for the reordering. The subscript for the array may be any numeric expression. If processing a multidimensional array, the usual rules apply to decide which dimension should be used. |
||||||||||||
N | ||||||||||||
Required. Any numeric expression giving the number of array elements to use in the sort. If this number is greater than either of the two array sizes, the number of elements used in the sort will be the lesser of the two array sizes. The maximum value for N is the size of the array. If the sum of N plus the starting element is greater than the size of the array, this computation will "wrap-around" and resume at element 0. For example if you sort 1,3,2 in ascending order starting at element 2 with an N equal to 3, the result would be 3,1,2. Demo[0] = 1; Demo[1] = 3; Demo[2] = 2; This may be useful in rare circumstances. |
||||||||||||
Descending | ||||||||||||
Required. Any numeric expression that indicates the ordering sequence to use in the sort. If it is true (non-0), the sort will be in descending (decreasing) order. If it is false (0), the sort will be in ascending (increasing) order. | ||||||||||||
TypeText | ||||||||||||
Optional. A numeric expression that controls the type of sort according to the following table. Defaults to zero - numeric sorting.
|
||||||||||||
CaseInsensitive | ||||||||||||
An optional parameter that accepts any logical expression. If CaseInsensitive resolves to true (1), the key array is treated as holding case-insensitive strings, thus allowing the caller to sort alphabetically instead of lexically. The default behavior is to sort case-sensitive (lexical sort). This parameter is ignored if TypeText is false (0). | ||||||||||||
Locale | ||||||||||||
Optional. A valid locale name (en-US or fr-FR). Additional modifiers are also allowed such as zh-CN_stroke (Chinese sorting using stroke order) The case insensitive flag ignored if doing a locale sort. As an alternative, set one of the following in the ComparisonFlags parameter: #LINGUISTIC_IGNORECASE or #NORM_IGNORECASE. |
||||||||||||
CmpFlags | ||||||||||||
A set of flags directing how the comparison should be done, as described in https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex Supported flags include: #NORM_IGNORECASE #NORM_IGNORENONSPACE #NORM_IGNORESYMBOLS #LINGUISTIC_IGNORECASE #LINGUISTIC_IGNOREDIACRITIC #NORM_IGNOREKANATYPE #NORM_IGNOREWIDTH #NORM_LINGUISTIC_CASING #SORT_DIGITSASNUMBERS #SORT_STRINGSORT |
Comments: |
Sort allows the re-ordering of a group of related arrays according to the order of a key array. If KeyArray and SortArray are the same array, the array is arranged in order.
Do not mix sorting modes. Some code may rely on sorting to be basic case-insensitive alphabetical (non-locale-aware) order, while other code may expect it to be locale-aware. Mixing these is likely to produce errors.
This statement performs a "partition sort", using an element of the array as the dividing point, such that all other elements are divided into those greater than and less than the dividing element. Each partition is then recursively sorted. As of VTS version 7.5, the first and middle elements of the array are swapped, making the middle element the partitioning element. This has increased performance gains for arrays whose sizes reach 100,000 or more elements.
|
Example 1:
If MatchKeys(2, "sort"); [ Sort(X[0], Y[0], 100, 0) { Sort the Y values first }; Sort(X[0], X[0], 100, 0) { Sort the X values }; ]
The two arrays mentioned in the previous section are sorted in preparation for plotting by the script, which is executed when the user types in "sort" from the keyboard.
Example 2:
Perhaps you have one array of tag names (N) and another array of matching tag descriptions (D). You want to sort both according to the descriptions. These arrays are shown in the following table in order to simplify the example:
N | D |
---|---|
B | 7 |
D | 1 |
A | 3 |
[
Sort(D[0], N[0], 3, 0); { sort entries in Names according to a sorted version of Descriptions }
Sort(D[0], D[0], 3, 0); { sort descriptions }
]
The result is:
N | D |
---|---|
D | 1 |
A | 3 |
B | 7 |