Click or drag to resize

ASortT Function (Array OfT, FuncT, T, Boolean)

Sort an array.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.4a
Syntax
 FUNCTION ASort<T>(
	aTarget AS ARRAY OF<T>,
	cbOrder AS Func<T, T, LOGIC>
)
WHERE T : NEW()
 AS ARRAY OF<T>
View Source

Parameters

aTarget
Type: Array OfT
The array to sort.
cbOrder
Type: FuncT, T, Boolean
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:
X#
1aArray := {3, 5, 1, 2, 4}
2ASort(aArray)                    // {1, 2, 3, 4, 5}
3ASort(aArray,,, {|x, y| x >= y})    // {5, 4, 3, 2, 1}
This example sorts a mixed-type array.
X#
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:
X#
1aArray := {"Fred", "Kate", "ALVIN", "friend"}
2ASort(aArray,,, {|x, y| Upper(x) <= Upper(y)})    // {ALVIN, FRED, FRIEND, KATE}
This example sorts a multidimensional array using the second element of each subarray:
X#
1aKids := {{"Mary", 14}, {"Joe", 23}, {"Art", 16}}
2aSortKids := ASort(aKids,,, {|x, y| x[2] <= y[2]})    // {{"Mary", 14}, {"Art", 16}, {"Joe", 23}}
Note:
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