DYNAMIC ARRAY.sort

Sorts the rows in the array.

Syntax

sort( key STRING, reverse BOOLEAN )
  1. key is the name of a member of a structured array (DYNAMIC ARRAY OF RECORD), or NULL if the array is not structured.
  2. reverse is FALSE for ascending order, TRUE for descending order.

Usage

This method sorts the array according to the name of the member passed as first parameter, for arrays defined with a structured type (DYNAMIC ARRAY OF RECORD). If the array is defined with a simple type, the first argument can be NULL.

The second parameter defines the sort order as ascending (FALSE) or descending (TRUE).

When doing subsequent calls to the sort() method using different record members of the array, the rows will be ordered by all of the record members specified for the cumulative sorts, with the most recent call defining the main sort field.

Another way to think of this is in terms of the ORDER BY clause of a SQL statement: If your dynamic array contained the variables A, B and C, and you included the following calls to the sort() method:

CALL a.sort("C",false)
CALL a.sort("B",false)
CALL a.sort("A",false)

This would be equivalent to writing an ORDER BY clause that states:

ORDER BY A, B, C
Note: Character string data is sorted according to the current application locale.

Example

MAIN
  DEFINE a DYNAMIC ARRAY OF RECORD
               key INTEGER,
               name VARCHAR(30)
         END RECORD
  LET a[1].key = 776236    LET a[1].name = "aaaaa"
  LET a[2].key = 273434    LET a[2].name = "cccccccc"
  LET a[3].key = 934092    LET a[3].name = "bbbbb"
  CALL a.sort("name",FALSE)
    -- Array is sorted by name (asc order)
  CALL a.sort("key",TRUE)
    -- Array is sorted by key (desc order), then by name (asc order)
    -- within each key; 
    -- The current sort becomes the main sort field, the initial sort
    -- becomes the secondary sort field
END MAIN