Click or drag to resize

Functions.ASort Method (Array, Usual, Usual, Usual)

Sort an array.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.16
	aTarget AS ARRAY,
	 nStart AS USUAL,
	 nCount AS USUAL,
	 cbOrder AS USUAL
Request Example View Source


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