Previous Topic Next topic Print topic


SORT Table Entries

A table sort using KEYS in OCCURS clause for sequencing:

 working-storage section.
 01 group-item. 
     05   tabl   occurs 10 times 
                     ascending elem-item2 
                     descending elem-item1. 
          10 elem-item1 pic x. 
          10 elem-item2 pic x. 
     . . . 
 procedure division. 
     . . . 
     sort tabl. 
     if tabl (1) . . .

This is a simple sort in which the table is sorted in ascending order using the key definitions in the OCCURS clause of data item Tabl to determine the sequence, that is Elem-Item2 would be the major key (ascending) and Elem-Item1 would be the secondary key (descending).

A table sort using the entire element for sequencing:

 working-storage section. 
 01 group-item. 
     05 tabl occurs 10 times 
         10 elem-item1 pic x. 
         10 elem-item2 pic x. 
     . . . 
 procedure division. 
     . . . 
     sort tabl ascending. 
     if tabl (1) ...

This is a simple sort in which the table is sorted in ascending order using each entire element of the table to determine the sequence.

A table sort with specified items for sequencing:

 working-storage section. 
 01 group-item. 
     05  tabl    occurs 10 times 
                      ascending elem-item3 
                      descending elem-item1. 
         10 elem-item1 pic x. 
         10 elem-item2 pic x. 
         10 elem-item3 pic x. 
     . . . 
 procedure division. 
     . . . 
     sort tabl descending elem-item2 elem-item3
     if tabl (1) ...

This is a sort in which the table is sorted based on specified key data items. The major key would be Elem-Item2, even though it was not specified as a KEY in the OCCURS clause. The secondary key would be Elem-Item3. It would be treated as a DESCENDING key for this sort because the DESCENDING (which is transitive across KEY data items) specified in the SORT statement would take precedence over the ASCENDING specified in the OCCURS clause.

A table sort for a nested table:

 working-storage section. 
 01 group-item. 
     05  tabl1   occurs 10 times 
                     indexed by t1-ind t2-ind. 
         10  tabl2 occur 5 times. 
             15 group1. 
                 20 elem-item1 pic x. 
             15 group2. 
                 20 elem-item1 pic 9. 
     . . . 
 procedure division. 
     . . . 
     set t1-ind to 3 
     sort tabl2 descending elem-item1 of group2 
     if group1 (3 1) ...

This sorts only the third instance of Tabl2, that is Tabl2(3). It uses the qualified data-item, Elem-Item1 of Group2 as its key. In normal Procedure Division references, Elem-Item1 of Group2 would require two levels of subscripting/indexing while in this reference it has none. (Similarly, Tabl2 normally requires one level of subscripting, but cannot be subscripted as data-name-2 in the SORT statement. Instead it uses the value of T1-Ind for determining which instance is to be sorted.)

Previous Topic Next topic Print topic