ASort Function (Array, Usual, Usual, Usual) | |
Sort an array.
Namespace:
XSharp.RT
Assembly:
XSharp.RT (in XSharp.RT.dll) Version: 2.19
Syntax FUNCTION ASort(
aTarget AS ARRAY,
nStart AS USUAL,
nCount AS USUAL,
cbOrder AS USUAL
) AS ARRAY
public static Array ASort(
Array aTarget,
[DefaultParameterValueAttribute(0, 1)] Usual nStart,
[DefaultParameterValueAttribute(0, 1)] Usual nCount,
[DefaultParameterValueAttribute(0, 1)] Usual cbOrder
)
Request Example
View SourceParameters
- aTarget
- Type: Array
The array to sort. - nStart
- Type: Usual
The starting element.
The default value is 1.
- nCount
- Type: Usual
The number of elements to process from nStart.
The default is all elements to the end of the array.
- cbOrder
- Type: Usual
A code block used to determine the sort order.
This argument is used to change the sorting order to descending or dictionary order.
Each time it is evaluated, two elements from the target array are passed as arguments.
The code block returns TRUE if the elements are in sorted order. See the examples below.
The default sorting order is ascending:
Elements with low values are sorted toward the beginning of the array (the first element).
Elements with high values are sorted toward the end of the array (the last element)
Return Value
Type:
Array
A reference to
aTarget.
Remarks
ASort() sorts all or part of an array.
The array may contains USUALs of mixed types.
Data types that can be sorted include character, date, logical, and numeric.
Strings are sorted in ASCII sequence; logical values are sorted with FALSE as the low value; date values are sorted chronologically; and numeric values are sorted by magnitude.
ASort() will not directly sort a multidimensional array.
To sort a multidimensional array, you must supply a code block which properly handles the subarrays.
Examples
This example creates an array of five unsorted elements, sorts the array in ascending order, then sorts the array in descending order using a code block:
1aArray := {3, 5, 1, 2, 4}
2ASort(aArray)
3ASort(aArray,,, {|x, y| x >= y})
This example sorts a mixed-type array.
1LOCAL a := {"Z", "A", "one", 2, 1, "Three"}
2ASort(a)
This example sorts an array of strings in ascending order, independent of case.
It does this by using a code block that converts the elements to uppercase before they are compared:
1aArray := {"Fred", "Kate", "ALVIN", "friend"}
2ASort(aArray,,, {|x, y| Upper(x) <= Upper(y)})
This example sorts a multidimensional array using the second element of each subarray:
1aKids := {{"Mary", 14}, {"Joe", 23}, {"Art", 16}}
2aSortKids := ASort(aKids,,, {|x, y| x[2] <= y[2]})
Tip |
---|
The < and > operators can be used in the codeblock if you are sure that there will be no duplicates; otherwise,
it is more appropriate to use <= and >=, as they properly allow for duplicate values.
|
See Also