Cobol SORT Verb
Most computer languages offer no direct support for sorting files --
that's a job usually reserved for system utilities. COBOL's SORT
verb reflects COBOL's orientation toward business data processing.
Typically a program using the SORT verb uses both an input procedure
and an output procedure. Otherwise there would be little reason to
do the sort within the program rather than a separate job step.
Even in this case, however, the SORT verb is not as useful as one might
think. It is usually better to split such a program into three separate
job steps: one corresponding to the input procedure and one
corresponding to the output procedure, with a sort step in between.
This approach offers several advantages:
-
Two small programs are easier to read and maintain than one big program.
-
Two small programs are easier to test. You can examine the output of
the input program, or manipulate the input of the output program.
-
You can more easily rearrange the smaller programs to fit changing
requirements. For example, the output of the input program may turn out
to be useful for some other purpose. The output program may find uses
in another context where the input program is not relevant, or where
the records are already sorted.
In older dialects of COBOL, input and output procedures carry some extra
baggage. Each procedure must be a COBOL section. If the section contains
more than a single paragraph, standard COBOL syntax all but forces you to
use a GO TO to exit the section. COBOL II and
later releases have removed this requirement.
When the SORT Verb is Appropriate
Despite the above considerations, the SORT verb may be the best solution
in some cases:
-
When only a minor aspect of the program requires a sort, and you don't
want that aspect to drive the design of the job;
-
When you need to sort things repeatedly, especially if you cannot
predict the number of sorts needed;
-
When performance is critical, and you can demonstrate that the
SORT verb provides better performance;
-
When the available utilities don't support the kind of sort you need.
In MS-DOS, for example, the standard sort utility is very limited. In
MVS this consideration does not apply.
COBOL Home
COBOL Style Forum