Monday, 28 July 2014

SORT in COBOL | COBOL SORT | COBOL Internal Sort | SORT


SORT IN COBOL
COBOL SORT 

In today’s tutorial, you’ll learn COBOL SORT. You’ll discover how to use the SORT verb to sort a sequential file in ascending or descending order. You learn how to use an INPUT PROCEDURE to filter or modify the records presented for sorting and how to use an OUTPUT PROCEDURE to process the sorted records instead of sending them directly to an output file. So, lets quickly jump on to the tutorial.

What is SORT?


The term SORT is used for a process that organizing file data into a specific sequence (i.e. ascending or descending) based on business requirements. 

In legacy applications, there is two sorting technique:  
  • INTERNAL SORT:   In internal sort, COBOL SORT VERB is used to sort file data.
  • EXTERNAL SORT: In external sort, external utilities such as JCL SORT, DB2 utility are used to sort file data, before it is feed into the COBOL program.


COBOL SORT
External SORT Utility

The SORT utility is completely different from, or external to the COBOL program and would be executed first if records needed to be in a sequence other than the sequence in which they were created. For an external utility, one has to just specify the key fields to sort on. As an alternative, COBOL has the SORT verb, which can make it very useful as a part of a COBOL program.

What is COBOL SORT?


COBOL provides very easy and efficient sorting methods for sorting data. The COBOL SORT verb is primarily used to sort sequential file data into a specific sequence for updating, answering inquiries, or generating reports.

You’re not required to write your own programs to sort the data in the desired sequence. With a simple COBOL SORT statement, you can sort the data in a file. You may even sort the data file in place. That is, you can take a file, sort it, and not create a separate output file.

Each sort in your program uses a Sort Work File, which contains the records as they are sorted by the system. You must declare these files in your program with a Select statement, like any other file. File organization and access modes are not specified for the file. However, you must select a unique filename.

COBOL SORT FORMAT:


The syntax for the simple SORT is given below. This version of SORT takes the records in the file name file, sorts them on the work-file (file-name-2) key or keys, and writes the sorted records to the Output file (i.e. file-name-3).

SORT in COBOL
COBOL SORT Syntax

 ASCENDING or DESCENDING KEY


The programmer must specify whether the key field is to be an ASCENDING KEY or a DESCENDING KEY, depending on which sequence is required:
  • ASCENDING: From lowest to highest.
  • DESCENDING: From highest to lowest. 

SEQUENCING RECORDS WITH MORE THAN ONE SORT KEY


The SORT verb may be used to sequence records with more than one key field. Suppose that we wish to sort a payroll file so that it is in ascending alphabetic sequence by name, within each level, for each office.

The following is a SORT statement that sorts records into ascending alphabetic NAME sequence within LEVEL-NO within OFFICE-NO:

SORT SORT-FILE
      ON ASCENDING KEY OFFICE-NO
      ON ASCENDING KEY LEVEL-NO
      ON ASCENDING KEY NAME
         USING PAYROLL-FILE-IN
         GIVING SORTED-PAYROLL-FILE-OUT

Because all key fields are independent, some key fields can be sorted in the ASCENDING sequence and others in the DESCENDING sequence. Note too that the words ON and KEY were not underlined in the instruction format, which means that they are optional words. If all key fields are to be sorted in ascending sequence, as in the preceding, we can condense the coding by using the phrase ON ASCENDING KEY only once. For example:

SORT SORT-FILE
      ON ASCENDING KEY MAJOR-KEY
                       INTERMEDIATE-KEY
                       MINOR-KEY
      .
      .
      .

What if two or more records have the same value in the SORT field (e.g., DEPT 01 is in two or more records)? With the most current version of COBOL, you can request the computer to put such records into the sort file in the same order that they appeared in the original input file. We add the WITH DUPLICATES IN ORDER clause to accomplish this:

SORT ...
      ON ASCENDING KEY ...
      WITH DUPLICATES IN ORDER
           USING ...
           GIVING ...

COBOL SORT PROGRAM Example :

SORT IN COBOL SAMPLE PROGRAM
COBOL SORT


Top Interview Questions.

  • What happens to the Sort Work File when the Sort is finished?
  • On most systems, the Sort Work File is automatically deleted after the processing associated with the Sort statement is complete.

  • I need to manipulate some fields for the Sort. Should I modify them in the Input Procedure or in the Output Procedure?
  • If any of the fields you are modifying are used as Sort Keys, you should modify them in the Input Procedure. Remember that records are not sorted as they go into the Input Procedure, so modifying the Sort Key will not adversely affect the Sort.

  • I noticed that in the examples, there were no performs outside of the Input and Output Procedures. Am I restricted in what may be performed?
  • Only slightly. You may not execute another Sort statement within an Input or Output Procedure. Also, you may not execute a Return within an Input Procedure, and you may not execute a Release in an Output Procedure. Otherwise, you are free to code any kind of statement or logic you desire. Just remember that the Input and Output Procedures are performed only once per sort.

  • I’m still a little unclear. Which file is actually sorted? Is it the input file?
  • No, it’s not the input file. The Sort Work File is the one that is sorted, which explains why you can manipulate data before the sort, using an Input Procedure. Using a Sort Work File also ensures that records returned from the Sort in the Output Procedure are in the sorted sequence.

Created with Artisteer

No comments:

Post a Comment