Compare Procedure

SORT calls the compare procedure to determine which of two records should be used next in the sorting process. Different keys or fields in the records can be compared; however, programs can be made more efficient by simplifying the individual keys and consolidating them into a single key. The comparison technique is determined entirely by you in the compare procedure.

In COBOL, the length and number of keys directly affects the amount of time required for comparison. A large number of keys scattered in the record should not be used because setup time increases comparison overhead. Arithmetic or numeric comparisons are generally faster than string comparisons. COBOL sorts should use the EBCDIC character set for string comparison whenever possible.

In ALGOL, the compare procedure should be coded to return TRUE when the two arrays are unequal and the first array must precede the second array. A FALSE should be returned when the arrays are equal or when the second array must precede the first array. In ALGOL sort operations, partial word comparisons are normally faster than string comparisons when characters within a word are tested.

SORT attempts to recognize when the input data is less than 40 percent in sequence and switches comparison either from ascending to descending or from descending to ascending. SORT remembers the change of mode and processes the data accordingly. The switch is done as often as necessary in order to produce longer strings. Given a set of input data in exact reverse sequence, SORT produces two strings—rather than the maximum use of strings—and completes the sort much faster.