Click or drag to resize

Functions.ASortT Method (Array OfT, Long, Long, FuncT, T, Logic)

Sort an array.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.18
	aTarget AS ARRAY OF<T>,
	nStart AS LONG,
	nCount AS LONG,
	cbOrder AS Func<T, T, LOGIC>
Request Example View Source


Type: Array OfT
The array to sort.
Type: Long
The starting element.
The default value is 1.
Type: Long
The number of elements to process from nStart.
The default is all elements to the end of the array.
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

The type of the array elements

Return Value

Type: Array OfT
A reference to aTarget.
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.
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)                    // {1, 2, 3, 4, 5}
3ASort(aArray,,, {|x, y| x >= y})    // {5, 4, 3, 2, 1}
This example sorts a mixed-type array.
1LOCAL  a := {"Z", "A", "one", 2, 1, "Three"}
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)})    // {ALVIN, FRED, FRIEND, KATE}
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]})    // {{"Mary", 14}, {"Art", 16}, {"Joe", 23}}
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