ASortT Function (Array OfT, Long, Long, FuncT, T, Logic) | |
Sort an array.
Namespace:
XSharp.RT
Assembly:
XSharp.RT (in XSharp.RT.dll) Version: 2.19
Syntax FUNCTION ASort<T>(
aTarget AS ARRAY OF<T>,
nStart AS LONG,
nCount AS LONG,
cbOrder AS Func<T, T, LOGIC>
)
AS ARRAY OF<T>
public static Array Of<T> ASort<T>(
Array Of<T> aTarget,
int nStart,
int nCount,
Func<T, T, bool> cbOrder
)
Request Example
View SourceParameters
- aTarget
- Type: Array OfT
The array to sort. - nStart
- Type: Long
The starting element.
The default value is 1.
- nCount
- Type: Long
The number of elements to process from nStart.
The default is all elements to the end of the array.
- cbOrder
- Type: FuncT, T, Logic
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)
Type Parameters
- T
- The type of the array elements
Return Value
Type:
Array OfT
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