Open In App

Internal Sort in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

Internal Sequential files are often used in big data operational processing applications, necessitating the records to be arranged in an operational ascending or descending order. This ensures quick access to the data and makes it easier to retrieve. Furthermore, sorting data in a file or combining multiple files is a common operational requirement across applications. To effectively execute sequential internal processing, sorting is employed as a technique to arrange the given records in the desired order to have easy retrieval, and for fast accessing data.

Organizing data in a file or combining multiple files is a common operational need found in many applications. Sorting is a method used to sequence the provided records in either ascending or descending order to facilitate sequential internal processing.

this technique is used to arrange the given records in either operational ascending or descending order to be able to execute the sequential Internal processing.

There are two techniques in COBOL to sort the files:

  • Internal Sort: This method sorts files within the COBOL programs. The SORT verb allows COBOL programs to sort the file before or after operations such as adding, updating, or deleting the records.
  • External Sort: This method uses the SORT utility in JCL to sort files. 

SORT Verb

The Sort verb takes input from a non-sequential file or internal procedure and produces output in an executed order to a file or internal procedure.  this produces resultant output in an executed sequence to a file or internal known as procedures After the files have been sorted, operations such as adding, updating or deleting records can be performed.

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

The SORT verb requires three distinct files to complete the sorting task:

  1. Input File: The Input File contains the records that need to be sorted.
  2. Work File: The Work File holds the records during the sorting process.
  3. Output File: The file is the result of the sorting, where the sorted records are stored, it is the final resultant output of the Sort verb

Syntax:

SORT work-files ON ASCENDING KEY res-key1  
 [ ON DESCENDING KEY res-key2 ]  
USING input-files GIVING output-files

SORT verb performs the following operations:

  1. The SORT verb initiates the opening of the input file in INPUT mode, the working file in I-O mode, and the output file in OUTPUT mode. 
  2. It then transfers the records from the input file to the working file for sorting. Depending on the user’s requirements, the SORT-FILE can be sorted in ascending or descending order.
  3. Sort like the SORT-FILE in ascending or descending records order as per like your requirement.
  4. It transfers the unique resultant sorted records from the work order file to the output file.
  5. The sorted, unique records are then transferred from the working file to the output file, and the input and output files are closed while the working file is deleted.

Merge Verb

The Merge verb in COBOL is used to compare the records from two or more easily sequenced files as well as classify them in one order. The operations for this include adding, updating, or deleting

Four Main files are used by the SORT verb for sorting in COBOL:

  1. Input Files
  2. Work File
  3. Output File

Syntax:

MERGE work-files ON ASCENDING KEY res-key1  
 [ ON DESCENDING KEY res-key2 ]  
USING input-1, input-2 GIVING output-files 

Merge Verb performs the following operations:

  1. The program begins by opening the input file in input mode, followed by the work file in I/O mode, and finally the output file in output mode.
  2. The process of moving the operational records from the input file to the work file for merging operations is known as a transfer.
  3. It merges all the files.
  4. The records from the work operations file are transferred to the final executing output file
  5. Finally, the input and output files are closed, and the work file is simply deleted.

Example 1:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. HELLOWORD.
Perform varying sort-counter from 1 by 1 until 
      sort-counter > total-records 
  
Display WS-SORT-KEYS(sort-counter) 
End-Perform
  
CLOSE IN-FILE
  
stop run
PROCEDURE DIVISION.
DISPLAY "HELLO WORLD".
STOP RUN.


Output:

Key1 Data1 
Key2 Data2 
Key3 Data3 
Key4 Data4 
Key5 Data5


Last Updated : 30 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads