Notes about COBOL Sort
The bare outline of COBOL sorting and merging is simple enough:
Sort Work-File
On Ascending Key Work-Key
Using Unsorted-File
Giving Sorted-File.
Here the key field (Work-Key) is part of the data record for the workspace file (Work-File).
Available Options
On Ascending Key Work-Key-1
Descending Key Work-Key-2
Some Available Special Registers
These are automatically allocated by the compiler for every COBOL program.
Move 'SORTOPUT' to Sort-Message.
For this to work, we need to have an output DD statement named SORTOPUT in the execution step.
Move 'MYSORTCS' to Sort-Control.
For this to work, we need to have an output DD statement named MYSORTCS in the execution step, containing DFSORT control statements. For instance:
//MYSORTCS DD *
OPTION FILSZ=E3500,SKIPREC=3
/*
Example of using special registers
Suppose we have the following in the JCL in the execution step:
//SORTCS DD *
OPTION SKIPREC=1
/*
//SYSOUT DD SYSOUT=*
//SORTOPUT DD SYSOUT=*
//PRINTER DD SYSOUT=*
Suppose we have the following in the Procedure Division:
Move 20 to Sort-File-Size.
Move 'SORTOPUT' to Sort-Message.
Move 'SORTCS' to Sort-Control.
Sort S-W-File
On Ascending Key ....
Using ....
Giving ....
Now:
Notice the use of the word OPTION in the SORTCS data set.
Compiler messages involved in sorting
If we use the Using or Giving options, an informational message is generated urging the use of the FASTSRT option to speed up the file input and/or output involved. If we were using large files, this might be useful. The default is NOFASTSRT.
If we use the Sort-File-Size special register, a warning is generated saying that the value provided might be overridden by a value found in the DFSORT FILSZ control statement in the Sort-Control data set.
An alternate use of the Sort verb
The Sort verb can also be used to sort tables. This requires the use of both an input procedure (which Releases one table entry at a time to the workspace file) and an output procedure (which Returns one record at a time from the workspace file and copies it into the table). This option may or may not be more work, in a given case, than writing code for a sorting algorithm.
In any case, this is a ridiculously slow way to sort a table. The table is in memory, and we are sorting it essentially by writing to a file, sorting the file, and reading the file back into the table.