SORT a File Using Input and Output Procedures

The SORT statement can be used to sort the records in a file. Note that SORT procedures should not be invoked by multiple threads simultaneously. The following program takes the names of the input and output files from the command line. RELEASE and RETURN statements are used in the input and output procedures, which input records, sort the records, and then output a sorted file.

 select ifile assign to ipf
     organization is line sequential 
     file status is ipstat.

 select sfile assign to "temp.dat".

 select ofile assign to opf 
     organization is line sequential.

 fd ifile. 
 01 irec     pic x(80).
 fd ofile. 
 01 orec     pic x(80). 
 sd sfile. 
 01 srec     pic x(80).

 working-storage section.
 01 ipstat.
   03 iskey1 pic x. 
   03 iskey2 pic x. 
 01 ipf  pic x(20). 
 01 opf  pic x(20). 
 01 ext  pic x(20). 
 01 clin     pic x(132). 
 01 len  pic 9(2) comp-x. 
 01 a    pic 9(2) comp-x value 0. 
 procedure division. 
     accept clin from command-line 
     unstring clin delimited by space into ipf, opf, ext
     if ext not = space 
         display "too many arguments"
     sort sfile on ascending srec 
      input procedure sortin 
      output procedure sortout

     stop run.

 sortin section. 
     open input ifile
     read ifile 
     perform until ipstat not = "00"
         move irec to srec 
         release srec 
         read ifile 
     close ifile.
 sortout section.
     return sfile at end go to sortout-exit
     display srec
     go to sortout.
     display "Done".