4D v14.3

SORT ARRAY

Home

 
4D v14.3
SORT ARRAY

SORT ARRAY 


 

SORT ARRAY ( array {; array2 ; ... ; arrayN}{; > or <} ) 
Parameter Type   Description
array  Array in Arrays to sort
> or <  Operator in ">" to sort in Ascending order, or "<" to sort in Descending order, or Ascending order if omitted

The SORT ARRAY command sorts one or more arrays into ascending or descending order.

Note: You cannot sort Pointer or Picture arrays. You can sort the elements of a two-dimensional array (i.e., a2DArray{$vlThisElem}) but you cannot sort the two-dimensional array itself (i.e., a2DArray).

The last parameter specifies whether to sort array in ascending or descending order. The “greater than” symbol (>) indicates an ascending sort; the “less than” symbol (<) indicates a descending sort. If you do not specify the sorting order, then the sort is ascending.

If more than one array is specified, the arrays are sorted following the sort order of the first array; no multi-level sorting is performed here. Instead you can use the MULTI SORT ARRAY command when you want to sort synchronized arrays.

The following example creates two arrays and then sorts them by company:

 ALL RECORDS([People])
 SELECTION TO ARRAY([People]Name;asNames;[People]Company;asCompanies)
 SORT ARRAY(asCompanies;asNames;>)

However, because SORT ARRAY does not perform multi-level sorts, you will end up with people‘s names in random order within each company. To sort people by name within each company, you would write:

 ALL RECORDS([People])
 ORDER BY([People];[People]Company;>;[People]Name;>)
 SELECTION TO ARRAY([People]Name;asNames;[People]Company;asCompanies)

You display the names from a [People] table in a floating window. When you click on buttons present in the window, you can sort this list of names from A to Z or from Z to A. As several people may have the same name, you also can use a [People]ID number field, which is indexed unique. When you click in the list of names, you will retrieve the record for the name you clicked. By maintaing a synchronized and hidden array of ID numbers, you are sure to access the record corresponding to the name you clicked:

  ` asNames array object method
 Case of
    :(Form event=On Load)
       ALL RECORDS([People])
       SELECTION TO ARRAY([People]Name;asNames;[People]ID number;alIDs)
       SORT ARRAY(asNames;alIDs;>)
    :(Form event=On Unload)
       CLEAR VARIABLE(asNames)
       CLEAR VARIABLE(alIDs)
    :(Form event=On Clicked)
       If(asNames#0)
  ` Use the array alIDs to get the right record
          QUERY([People];[People]ID Number=alIDs{asNames})
  ` Do something with the record
       End if
 End case
 
  ` bA2Z button object method
  ` Sort the arrays in ascending order and keep them synchronized
 SORT ARRAY(asNames;alIDs;>)
 
  ` bZ2A button object method
  ` Sort the arrays in descending order and keep them synchronized
 SORT ARRAY(asNames;alIDs;<)

 
PROPERTIES 

Product: 4D
Theme: Arrays
Number: 229

 
HISTORY 

Created: < 4D v6

 
SEE ALSO 

MULTI SORT ARRAY
ORDER BY
SELECTION TO ARRAY

 
ARTICLE USAGE

4D Language Reference ( 4D v11 SQL Release 6)
4D Language Reference ( 4D v12.4)
4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v13.5)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)