Thursday, August 12, 2021

SORT table or array using COBOL SORT statement

COBOL SORT statement can also be used for sorting tables (arrays). It sorts table elements according to specified table keys.

Below is a sample program to sort a table
 
WORKING-STORAGE SECTION.                                  
01 WS-FILLER.                                             
    05 WS-TBL  OCCURS 10  TIMES.                          
        10 WS-USE-CNT     PIC 9(4)   COMP.                
01 WS-I                   PIC 9(4)   COMP.                
PROCEDURE DIVISION.                                       
   MOVE 112  TO WS-USE-CNT (1)                            
   MOVE 13   TO WS-USE-CNT (2)                            
   MOVE 55   TO WS-USE-CNT (3)                            
   MOVE 15   TO WS-USE-CNT (4)                             
   MOVE 16   TO WS-USE-CNT (5)                            
   MOVE 3    TO WS-USE-CNT (6)                            
   MOVE 43   TO WS-USE-CNT (7)                            
   MOVE 78   TO WS-USE-CNT (8)                             
   MOVE 34   TO WS-USE-CNT (9)                            
   MOVE 46   TO WS-USE-CNT (10)                           
   SORT WS-TBL ASCENDING WS-USE-CNT                       
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10       
      DISPLAY WS-USE-CNT (WS-I)                           
   END-PERFORM                                            
   GOBACK.
 
Output of the above code is
 
0003
0013
0015
0016
0034
0043
0046
0055
0078
0112
 
Another example is given below
 
WORKING-STORAGE SECTION.
01 GROUP-ITEM.
05 TABL OCCURS 10 TIMES
10 ELEM-ITEM1 PIC X.
10 ELEM-ITEM2 PIC X.
10 ELEM-ITEM3 PIC X.
...
PROCEDURE DIVISION.
...
SORT TABL DESCENDING ELEM-ITEM2 ELEM-ITEM3.
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.