Sorting a table

You can sort a table by using the format 2 SORT statement. It is part of the 2002 COBOL Standard.

About this task

The format 2 SORT statement sorts table elements according to the specified table keys, and it is especially useful for tables used with SEARCH ALL. You can specify the keys for sorting as part of the table definition, which can also be used in the SEARCH ALL statement. Alternatively, you can also specify the keys for sorting as part of the SORT statement, either if you want to sort the table using different keys than those specified in the table definition, or if the table has no keys specified.

With the format 2 SORT statement, you don't need to use the input and output procedures as you do with the format 1 SORT statement.

See the following example in which the table is sorted based on specified keys:
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.
    IF TABL (1)...

Related references  
SORT statement (COBOL for Linux® on x86 Language Reference)  
Using the format 2 SORT statement to sort a table