SORT ARRAY

4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next

version 3


SORT ARRAY (array{; array2; ...; arrayN}{; > or <})

ParameterTypeDescription
arrayArrayArrays to sort
> or <> to sort in Ascending order, or
< to sort in Descending order, or
Ascending order if omitted

Description

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. This feature is especially useful with grouped scrollable areas in a form; SORT ARRAY maintains the synchronicity of the arrays that sustain the scrollable areas.

Examples

1. 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)

2. 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;<)

See Also

MULTI SORT ARRAY, ORDER BY, SELECTION TO ARRAY.


4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next