skip to main content

Procedure Division Statements : SORT

SORT
Format 1
SORT File-Name-1 { { ON {ASCENDING } KEY {Data-Name-1} ... } ... }
  { KEY AREA IS Key-Table } 
  [ WITH DUPLICATES IN ORDER ] 
  [ COLLATING SEQUENCE {IS Alphabet-Name-1 [Alphabet-Name-2] }
  { INPUT PROCEDURE IS Procedure-Name-1 [{THROUGH} Procedure-Name-2] } 
  { USING { File-Name-2 } ...                                        } 
  { OUTPUT PROCEDURE IS Procedure-Name-3 [{THROUGH} Procedure-Name-4] } 
  { GIVING { File-Name-3 } ...                                        }
Format 2
  [ COLLATING SEQUENCE {IS Alphabet-Name-1 [Alphabet-Name-2] }
Syntax rules
All Formats
1.
2.
3.
4.
5.
01  KEY-TABLE.
    03  SORT-KEY OCCURS N TIMES.   
        05  KEY-ASCENDING   PIC X  COMP-X.   
        05  KEY-TYPE        PIC X  COMP-X.   
        05  KEY-OFFSET      PIC XX COMP-X.   
        05  KEY-SIZE        PIC XX COMP-X.   
        05  KEY-DIGITS      PIC X  COMP-X.
Format 1
6.
7.
8.
A.
B.
C.
D.
E.
F.
9.
10.
11.
12.
13.
14.
If you provide invalid data in the key-table, results are undefined.
Format 2
15.
16.
A.
B.
C.
17.
General rules
All Formats
1.
2.
3.
A.
B.
C.
4.
Format 1
5.
A.
B.
6.
A.
B.
C.
7.
A.
B.
C.
8.
9.
A.
B.
C.
 
The value of the data item referenced by the DEPENDING ON phrase of a RECORD IS VARYING clause specified in the file description entry for file-name-2 is undefined upon completion of the SORT statement.
10.
11.
 
NOTE - This return mechanism transfers control from the end of the output procedure and is not associated with the RETURN statement.
12.
A.
B.
 
For a relative file, the relative key data item for the first record returned has the value 1; for the second record returned, the value 2; etc. After execution of the SORT statement, the content of the relative key data item indicates the last record returned to the file.
C.
 
The value of the data item referenced by the DEPENDING ON phrase of a RECORD IS VARYING clause specified in the sort-merge file description entry for file-name-1 is undefined upon completion of the SORT statement for which the GIVING phrase is specified.
13.
A.
B.
Format 2
14.
15.
A.
B.
C.
16.
17.
18.
19.
20.
21.
Examples
Format 1 - Sort sequential file into another sequential file by alternate key
 input-output section.
 file-control.
   select sort-file
          assign to sort.
   select seq-1-file
          assign to disk "seq1.dat"
          binary sequential
          status is seq-1-status.
   select seq-2-file
          assign to disk "seq2.dat"
          binary sequential.
 file section.
 fd  seq-1-file.
 01  seq-1-record.
     03  seq-1-key                       pic 9(10).
     03  seq-1-alt-key.
         05  seq-1-alt-key-a             pic x(30).
         05  seq-1-alt-key-b             pic 9(10).
     03  seq-1-body                      pic x(50).
 fd  seq-2-file.
 01  seq-2-record                        pic x(100).
 sd  sort-file.
 01  sort-record.
     03  sort-key                        pic x(10).
     03  sort-alt-key                    pic x(40).
     03  filler                          pic x(50).
 procedure division.
   sort sort-file on ascending key sort-alt-key
        using seq-1-file giving seq-2-file.
Format 2 - Sort an array of customers
 working-storage section.
 77 i pic 9(3).
 01 cust-array occurs 5 times.
    05 cust-code pic x(3).
    05 cust-name pic x(20).
    05 cust-city pic x(20).
 procedure division.
   perform fill-array
   perform display-array
   perform sort-array-code
   perform display-array
   perform sort-array-name
   perform display-array
   goback.
   move "444" to cust-code(1)
   move "Adam Smith" to cust-name(1)
   move "New York" to cust-city(1)
   move "222" to cust-code(2)
   move "Eve Lion" to cust-name(2)
   move "Los Angeles" to cust-city(2)
   move "111" to cust-code(3)
   move "Walter Darryn" to cust-name(3)
   move "Chicago" to cust-city(3)
   move "333" to cust-code(4)
   move "Lola Lyn" to cust-name(4)
   move "Washington" to cust-city(4)
   move "555" to cust-code(5)
   move "Will Smith" to cust-name(5)
   move "Riverside" to cust-city(5)
   display "------ Customers "
   perform varying i from 1 by 1 until i > 5
     display cust-code(i) " " cust-name(i) " " cust-city(i)
   end-perform
   sort cust-array on ascending key cust-code.
   sort cust-array on descending key cust-name.

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left