SORT Statement COBOL

The SORT statement creates a sort file by executing an input procedure or by transferring records from another file, sorts the records in the sort file on a set of specified keys, and in the final phase of the sort operation, makes available each record from the sort file, in sorted order, to an output procedure or to an output file.

SORT Statement

A SORT statement may appear anywhere in the Procedure Division except in the declaratives portion.

file-name-1 must be described in a sort-merge file description entry in the Data Division.

data-name-1 may be qualified.

data-name-1 must reference either a record-name associated with file-name-1 or a data item in a record associated with file-name-1.If more than one record description entry is associated with file-name-1 , the data items referenced by different specifications of data-name-1 need not all be associated with the same record description entry.

The data item referenced by data-name-1 must not be a group item that contains a variable-occurrence data item.

file-name-2 and file-name-3 must be described in a file description entry in the Data Division.

The files referenced by file-name-2 and file-name-3 may reside on the same multiple file reel (or reels).

No pair of file-names in the same SORT statement may be specified in the same SAME SORT AREA or SAME SORT-MERGE AREA clause. (See the I-O-CONTROL paragraph.)

The words THRU and THROUGH are synonymous.

If the USING phrase is specified and the file referenced by file-name-1 contains variable-length records, the size of the records contained in the file referenced by file-name-2 must not be shorter than the shortest record nor longer than the longest record described for file-name-1. If the file referenced by file-name-1 contains fixed-length records, the size of the records contained in the file referenced by file-name-2 must not be longer than the fixed record size specified for the file referenced by file-name-1.

If the GIVING phrase is specified and the file referenced by file-name-3 contains variable-length records, the size of the records contained in the file referenced by file-name-1 must not be shorter than the shortest record nor longer than the longest record described for file-name-3. If the file referenced by file-name-3 contains fixed-length records, the size of the records contained in the file referenced by file-name-1 must not be longer than the fixed record size specified for the file referenced by file- name-3.

General Rules

The general rules that apply to the SORT statement are as follows:

  1. The SORT statement releases all the records in the file referenced by file-name-2 or released by an input procedure to the file referenced by file-name-1, and returns them to an output procedure, or to the file referenced by file-name-3, in an order determined by the ASCENDING and DESCENDING phrases and the values of the data items referenced by the specifications of data-name-1.
  2. The words ASCENDING and DESCENDING apply to each subsequent occurrence of data-name-1 until another word ASCENDING or DESCENDING is encountered.
  3. The data items referenced by the specifications of data-name-1 are the key data items that determine the order in which records are returned from the file referenced by file-name-1. The order of significance of the keys is the order in which they are specified in the SORT statement, without regard to their association with ASCENDING or DESCENDING phrases. The first (or only) key data item is the most significant. Further key data items, if any, are of progressively lesser significance.
  4. To determine the relative order in which two records are returned from the file referenced by file-name-1, the contents of corresponding key data items are compared according to the rules for comparison of operands in a relation condition, starting with the most significant key data item.
    • If the contents of the corresponding key data items are not equal and the key is associated with the ASCENDING phrase, the record containing the key data item with the lower value is returned first.
    • If the contents of the corresponding key data items are not equal and the key is associated with the DESCENDING phrase, the record containing the key data item with the higher value is returned first.
    • If the contents of the corresponding key data items are equal, the determination is made on the contents of the next most significant key data item.
  5. If the DUPLICATES phrase is specified and the contents of all the key data items in one record are equal to the contents of the corresponding key data items in one or more other records, the order of the return of such duplicate-key records is:
    • When an input procedure is not specified, the order of the associated input files is specified in the SORT statement. Within a given input file the order is that in which the records are accessed from that file.
    • When an input procedure is specified, the order in which these records are released by that input procedure.
  6. If the DUPLICATES phrase is not specified, the order in which duplicate-key records are returned is not predictable.
  7. The collating sequence that applies to the comparison of nonnumeric key data items is determined at the beginning of the execution of the SORT statement in the following order of precedence:
    • The collating sequence established by the COLLATING SEQUENCE phrase, if specified, in the SORT statement.
    • The collating sequence established as the program collating sequence.
  8. The execution of the SORT statement consists of three distinct phases as follows:
    • Records are made available to the file referenced by file-name-1 .This is achieved either by the execution of RELEASE statements in the input procedure or by the implicit execution of READ statements for file-name-2. When this phase commences, the file referenced by file-name-2 must not be in the open mode. When this phase terminates, the file referenced by file-name-2 is not in the open mode.
    • The file referenced by file-name-1 is sequenced. No processing of the files referenced by file-name-2 and file-name-3 takes place during this phase.
    • The records of the file referenced by file-name-1 are made available in sorted order. The sorted records are either written to the file referenced by file-name-3 or, by the execution of a RETURN statement, are made available for processing by the output procedure. When this phase commences, the file referenced by file-name-3 must not be in the open mode. when this phase terminates, the file referenced by file-name-3 is not in the open mode.
  9. The input procedure may consist of any procedure needed to select, modify or copy the records that are made available one at a time by the RELEASE statement to the file referenced by file-name-1 .The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT without the optional PROGRAM phrase, GO TO and PERFORM statements in the range of the input procedure, as well as all statements in declarative procedures that are executed as a result of the execution of statements in the range of the input procedure. The range of the input procedure must not cause the execution of any MERGE, RETURN or SORT statement.
  10. If an input procedure is specified, control is passed to the input procedure before the file referenced by file-name-1 is sequenced by the SORT statement. The compiler inserts a return mechanism at the end of the last statement in the input procedure and when control passes the last statement in the input procedure, the records that have been released to the file referenced by file-name-1 are sorted.
  11. If the USING phrase is specified, all the records in the file (or files) referenced by file-name-2 are transferred to the file referenced by file-name-1. For each of the files referenced by file-name-2 the execution of the SORT statement causes the following actions to be taken:
    • The processing of the file is initiated.The initiation is performed as if an OPEN statement with the INPUT phrase had been executed.
    • The logical records are obtained and released to the sort operation. Each record is obtained as if a READ statement with the NEXT the AT END phrases had been executed. If the file referenced by file-name-1 contains fixed-length records, any record in the file referenced by file-name-2 containing fewer character positions than that specified for file-name-1is space-filled on the right beginning with the first character position after the last character in the record when that record is released to the file referenced by file-name-1.
    • For a relative file, the contents of the relative key data item are undefined after the execution of the SORT statement if file-name-2 is not referenced in the GIVING phrase.
    • The processing of the file is terminated. The termination is performed as if a CLOSE statement without optional phrases had been executed. This termination is performed before the file referenced by file-name-1 is sequenced by the SORT statement.
  12. These implicit functions are performed such that any associated USE procedures are executed; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by file-name-2 or accessing the record area associated with file-name-2.

  13. The output procedure may consist of any procedure needed to select, modify or copy the records that are made available one at a time by the RETURN statement in sorted order from the file referenced by file-name-1. The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT without the optional PROGRAM phrase, GO TO and PERFORM statements in the range of the output procedure, as well as all statements in declarative procedures that are executed as a result of the execution of statements in the range of the output procedure. The range of the output procedure must not cause the execution of any MERGE, RELEASE, or SORT statement.
  14. If an output procedure is specified, control passes to it after the file referenced by file-name-1 has been sequenced by the SORT statement.The compiler inserts a return mechanism at the end of the last statement in the output procedure and when control passes the last statement in the output procedure, the return mechanism provides for termination of the sort and then passes control to the next executable statement after the SORT statement. Before entering the output procedure, the sort procedure reaches a point at which it can select the next record in sorted order when requested. The RETURN statements in the output procedure are the requests for the next record.
  15. If the GIVING phrase is specified, all the sorted records are written on the file referenced by file-name-3 as the implied output procedure for the SORT statement. At the start of execution of the SORT statement, the file referenced by file-name-3 must not be in the open mode. For each of the files referenced by file-name-3, the execution of the SORT statement causes the following actions to be taken:
    • The processing of the file is initiated. The initiation is performed as if an OPEN statement with the OUTPUT phrase had been executed. The initiation occurs after the execution of the input procedure, if there is one.
    • The sorted logical records are returned and written onto the file. Each record is written as if a WRITE statement without any optional phrases had been executed.
    • For a relative file, the relative key data item for the first record returned contains the value 1; for the second record returned, the value 2, and so forth. After execution of the SORT statement, the contents of the relative key data item indicate the last record returned to the file.
    • The processing of the file is terminated. The termination is performed as if a CLOSE statement without optional phrases had been executed.
  16. These implicit functions are performed such that any associated USE procedures are executed. However, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by, or accessing the record area associated with, file-name-3. On the first attempt to write beyond the externally defined boundaries of the file, any USE procedure specified for the file is executed. If control is returned from that USE procedure or if no such USE procedure is specified, the processing of the file is terminated as described above.

  17. Segmentation can be applied to programs containing the SORT statement. However,the following restrictions apply:
  • If a SORT statement appears in a section that is not in an independent segment, any input procedures or output procedures referenced by that SORT statement must appear:
  • (1)Totally within nonindependent segments, or

    (2)Wholly contained in a single independent segment.

  • If a SORT statement appears in an independent segment, any input procedures or output procedures referenced by that SORT statement must be contained:
  • (1)Totally within nonindependent segments, or

    (2)Wholly within the same independent segment as that SORT statement.

SORT Statement Example



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics