Compare Procedure

<compare procedure>

──<procedure identifier>───────────────────────────────────────────────┤

The compare procedure is called by the sort intrinsic to apply the appropriate sort criteria to a pair of input records. The procedure must be a Boolean procedure, must not be declared EXTERNAL, and must have exactly two parameters. Each of the parameters must be a one-dimensional array with a lower bound of zero. Every time two input records are to be compared, the sort intrinsic calls the compare procedure and passes the two records to the compare procedure through the array parameters. If the compare procedure returns TRUE, the record passed to the first array precedes, in the sorted output, the record passed to the second array. If the compare procedure returns FALSE, the record passed to the second array precedes the record passed to the first array. The compare procedure must not contain any SORT or MERGE statement.

The following is an example of a compare procedure that can be used to sort arithmetic data in ascending sequence:

BOOLEAN PROCEDURE CMP(A,B);
ARRAY A,B[0];
   BEGIN
   CMP:= A[0] < B[0];
   END CMP;

For alphanumeric comparisons, the following compare procedure can be used to sort data in ascending sequence:

BOOLEAN PROCEDURE CMP(A,B);
ARRAY A,B[0];
   BEGIN
   CMP:= POINTER(A) LSS POINTER(B) FOR 6;
   END CMP;

The CMP procedures above return TRUE if the value in A[0] compares as less than the value in B[0] and return FALSE if the value in A[0] compares as greater than or equal to the value in B[0]. Therefore, if A[0] is less than B[0], the content of array A is passed to the output file or procedure before the content of array B, and if A[0] is greater than or equal to B[0], the content of array B is passed to the output file or procedure before the content of array A. If either of these compare procedures is used, word zero of the input records is considered to be the key on which sorting is done.

For the actual comparison, a string relation can be used to compare a string from each record (according to the EBCDIC collating sequence), or an arithmetic relation can be used to compare an arithmetic value from each record. The comparison can be done on one or more fields, called keys, from each record or on the entire record. The manner in which the comparison is done is specified entirely by the programmer.