Home » COBOL Internal Sort

COBOL Internal Sort

by Online Tutorials Library

COBOL – Internal Sort

Sequential files are used very commonly in data processing applications. The records in these files usually need to be put in ascending or descending order for proper, easy, and fast accessing.

Sorting data in a file or combining two or more files is a common requirement in nearly all applications. Sorting is a technique used to arrange the records in either ascending or descending order to be able to perform the sequential processing.

There are two techniques available in COBOL to sort the files:

External Sort: This is used to sort files with the help of SORT utility in JCL.

Internal Sort: This method sorts files within a COBOL program. COBOL provides the SORT verb to sort a file.

SORT Verb

Sort verb accepts input from a non-sequential file or internal procedure and produces output in a requested sequence to a file or internal procedure. Before or after they are sorted, you can add, update, or delete the records.

Three files are used by the SORT verb to perform the sorting in COBOL:

  • Input File: This is the file to be sorted in either ascending or descending order.
  • Work File: This file holds records while the process of sorting is ongoing. To perform the sorting operation, all the input files’ records are transferred to the work file. The file needs to be defined in the File-section under SD (Sort file Description Level) entry.
  • Output File: Output file is the result file in which the final result of the sorting process will store. Means, this file is the final output of the Sort verb.

Syntax:

SORT verb performs the following operations:

  • It opens the input file in the INPUT mode, work file in the I-O mode, and output file in the OUTPUT mode.
  • It transfers the records from the input file to the work file for the sorting operation.
  • Sorts the SORT-FILE in ascending or descending order as per your requirement.
  • It transfers the sorted records from the work file to the output file.
  • Closes the input and output file, and deletes the work file.

Merge Verb

Merge verb is used to compare the records from two or more sequenced files and combine them in order. You can add, update, or delete the records after they are merged.

Four files are used by the SORT verb to perform the sorting in COBOL:

  • Input Files: Two Input files are required to perform the merge operation.
  • Work File
  • Output File

Syntax:

Merge Verb performs the following operations:

  • It opens the input file in the INPUT mode, work file in the I-O mode, and output file in the OUTPUT mode.
  • It transfers the records from the input file to the work file for the merging process.
  • It merges the file.
  • It transfers the merged records from the work file to the output file.
  • Closes the input and output file, and deletes the work file.

You may also like