sort_number_array

The function sort_number_array sorts a number array. The sorted elements are stored in a second array.

sort_number_array ( input;output;number of elements to be sorted )

sort_number_array is of type BOOL.

The function has three parameters:

  1. input; ARRAY of type NUMBER. This array contains the elements that must be sorted.
  2. output; ARRAY of type NUMBER. This array will receive the sorted elements.
  3. number of elements to sort; NUMBER. This is the number of elements in the array, which must be sorted starting from the first element. This can either be a number or a NUMBER variable or expression.

Return values

The function sort_number_array will return TRUE if the array was sorted successfully, and FALSE if 'number of elements to be sorted' is negative or zero.

If the sort_number_array function fails the output array is unmodified. The input array is not modified by the function. The output array will receive the sorted values from the input array. If the output array is larger than the number of sorted elements, those elements outside the sorted range are left unmodified.

Example

ARRAY NUMBER input [4]
ARRAY NUMBER output[4]
NUMBER i

ASSIGN input[1] := 4
ASSIGN input[2] := 2
ASSIGN input[3] := 1
ASSIGN input[4] := 3
IF sort_number_array (input; output; 4) THEN
FOR i FROM 1 UPTO 4

DO
#
@(i) @(output[i])
#
OD

ELSE
#
Failed to sort the input.
#
STOP
FI

Here the fact that the sort_number_array is a function of type BOOL is used to implement error handling. If the sort succeeds the return value of the function will be TRUE, the IF statement becomes TRUE, and the THEN part is executed. If the return is FALSE the ELSE part of the IF statement is executed.