Function ASort() Foundation
Sorts the elements of an array.
ASort( <aArray>, [<nStart>], [<nCount>], [<bOrder|lOrder>] ) --> aArray
The return value of ASort() is a reference to <aArray>.
The array function ASort() sorts the elements of an array. One dimensional arrays are sorted in ascending order by default. This means that smaller values are placed at the beginning of the array, with larger values at the end. Sorting in descending order or sorting multi-dimensional arrays requires that a code block be specified. The code block must contain a logical expression comparing two values. Two values x1 and x2 are passed to the code block from ASort(). To create ascending order, the code block must return the result of evaluating whether x1 < x2. To create descending order, the code block must return the result of evaluating whether X1 > X2.
Character strings are sorted according to their lexical order. Sorting depends on the settings SET EXACT, SET LEXICAL and SET COLLATION. Logical values are sorted with .F. (false) being less than .T. (true). Date values are sorted chronologically and numeric values according to their size.
// The example shows the ascending and descending sorting of a
// one dimensional array.
PROCEDURE Main
LOCAL aArray
aArray := {"E","A","D","C","B"}
// sort ascending
ASort( aArray ) // aArray is: {"A","B","C","D","E"}
// sort descending
ASort( aArray,,, .T. )
// aArray is: {"E","D","C","B","A"}
RETURN
// The example shows the ascending and descending sorting of a
// two dimensional array.
PROCEDURE Main
LOCAL aArray
aArray := { ;
{"E", 4 }, ;
{"A", 3 }, ;
{"D", 2 }, ;
{"C", 5 }, ;
{"B", 1 } ;
}
// first column ascending sort
ASort( aArray,,, {|aX,aY| aX[1] < aY[1] } )
// aArray is: { {"A", 3 }, ;
// {"B", 1 }, ;
// {"C", 5 }, ;
// {"D", 2 }, ;
// {"E", 4 } }
// second column descending sort
ASort( aArray,,, {|aX,aY| aX[2] > aY[2] } )
// aArray is: { {"C", 5 }, ;
// {"E", 4 }, ;
// {"A", 3 }, ;
// {"D", 2 }, ;
// {"B", 1 } }
RETURN
// In the example, a UDF AIndex() is shown which creates an
// index array for a two dimensional data array, analogous to an
// index file. The index array contains numeric pointers into the data
// array. The index array is sorted on the basis of the values in
// the data array.
#include "Directry.ch"
PROCEDURE Main
LOCAL aData, aNameIndex, aDateIndex
aData := Directory("*.*")
aNameIndex := AIndex( aData, F_NAME )
aDateIndex := AIndex( aData, F_DATE )
** Output of file name and date in normal order
AEval( aData, {|a| QOut(a[F_NAME],a[F_DATE]) } )
** Output of file name and date sorted by name
AEval( aNameIndex, {|n| QOut(aData[n,F_NAME], ;
aData[n,F_DATE]) } )
** Output of file name and date sorted by date
AEval( aDateIndex, {|n| QOut(aData[n,F_NAME], ;
aData[n,F_DATE]) } )
RETURN
FUNCTION AIndex( aData, nSubScript )
LOCAL aIndex[ Len(aData) ] // create array index
// fill index array with
// numeric pointers
AEval( aIndex, {|a,i| aIndex[i]:=i } )
// sort index array on the basis
// of the values in the data array
RETURN ASort( aIndex,,, {|n1,n2| aData[n1, nSubScript] < ;
aData[n2, nSubScript] } )
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.