Version 4.2.6 for Mainframes
 —  Statements  —

SORT

Structured Mode Syntax

END-ALL                    
[AND]                    
SORT

THEM

[BY]

operand1

ASCENDING

10
RECORDS DESCENDING
    USING-clause                
    [GIVE-clause]                
    statement                
END-SORT                    

* If a statement label is specified, it must be placed before the keyword SORT, but after END-ALL (and AND).

Reporting Mode Syntax

SORT

THEM

[BY]

operand1

ASCENDING

10
RECORDS DESCENDING
    [USING-clause]                
    [GIVE-clause]                
    statement                
[LOOP]                  

This document covers the following topics:

For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.

Related Statement: FIND with SORTED BY option

Belongs to Function Group: Loop Execution


Function

The SORT statement is used to perform a sort operation, sorting the records from all processing loops that are active when the SORT statement is executed.

For the sort operation, Natural's internal sort program is used. It is also possible to use another, external sort program.

Top of page

Restrictions

Top of page

Syntax Description

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand1   S       A N P I F B D T         no no

Syntax Element Description:

END-ALL

In structured mode, the SORT statement must be preceded by END-ALL, which serves to close all active processing loops. The SORT statement itself initiates a new processing loop, which must be closed with END-SORT.

Note:
For reporting mode: The SORT statement closes all active processing loops and initiates a new processing loop.

operand1
Sort Criteria:

operand1 represents the fields/variables to be used as the sort criteria. 1 to 10 database fields (descriptors and non-descriptors) and/or user-defined variables may be specified. A multiple-value field or a field contained within a periodic group may be used. A group or an array is not permitted.

ASCENDING

The default sort sequence is ascending. If you wish the values to be sorted in descending sequence, specify DESCENDING.

ASCENDING/DESCENDING may be specified for each sort field.

DESCENDING
USING See USING Clause below.
GIVE See GIVE Clause below.
END-SORT The Natural reserved word END-SORT must be used to end the SORT statement.

USING Clause

The USING clause indicates the fields which are to be written to intermediate sort storage. It is required in structured mode and optional in reporting mode. However, it is strongly recommended to also use it in reporting mode so as to reduce memory requirements.

USING {operand2}...

USING KEYS

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand2   S A     A N P I F B D T L C     no no

Syntax Element Description:

USING operand2 You can specify additional fields that are to be written to intermediate sort storage - in addition to the sort key fields (as specified with operand1).
USING KEYS Only the sort key fields, as specified with operand1, will be written to intermediate sort storage.

In Reporting Mode: If you omit the USING clause, all database fields of processing loops initiated before the SORT statement, as well as all user-defined variables defined before the SORT statement, will be written to intermediate sort storage.

If, after sort execution, a reference is made to a field which was not written to sort intermediate storage, the value for the field will be the last value of the field before the sort.

GIVE Clause

The GIVE clause is used to specify Natural system functions (MAX, MIN, etc.) that are to be evaluated in the first phase of the SORT statement. These system functions may be referenced in the third phase (see SORT Statement Processing). A reference to a system function after the SORT statement must be preceded by an asterisk, for example, *AVER(SALARY).

 

MAX          
  MIN          
  NMIN          
  COUNT          
GIVE NCOUNT [OF]

(operand3 )

[(NL=nn)]
OLD operand3
  AVER          
  NAVER          
  SUM          
  TOTAL          

Operand Definition Table:

Operand Possible Structure Possible Formats Referencing Permitted Dynamic Definition
operand3   S A     *                       yes no

* depends on function

Syntax Element Description:

MAX | MIN | NMIN | COUNT | NCOUNT | OLD | AVER | NAVER | SUM | TOTAL For details on the individual system functions, see the System Functions documentation.
operand3 operand3 is the field name.
(NL=nn)

This option applies to AVER, NAVER, SUM and TOTAL only and will be ignored for any other system function.

This option may be used to prevent an arithmetic overflow during the evaluation of system functions; it is described under Arithmetic Overflows in AVER, NAVER, SUM or TOTAL in the System Functions documentation.

Top of page

Three-Phase SORT Processing

A program containing a SORT statement is executed in three phases.

1st Phase - Selecting the Records to be Sorted

The statements before the SORT statement are executed. Data as described in the USING clause will be written to intermediate sort storage.

In reporting mode, any variables to be used as accumulators following the sort must not be defined before the SORT statement. In structured mode, they must not be included in the USING clause. Fields written to intermediate sort storage cannot be used as accumulators because they are read back with each individual record during the 3rd processing phase. Consequently, the accumulation function would not produce the desired result because with each record the field would be overwritten with the value for that individual record.

The number of records written to intermediate storage is determined by the number of processing loops and the number of records processed per loop. One record on the internal intermediate storage is created each time the SORT statement is encountered in a processing loop. In the case of nested loops, a record is only written to intermediate storage if the inner loop is executed. If in the example below a record is to be written to intermediate storage even if no records are found for the inner (FIND) loop, the FIND statement must contain an IF NO RECORDS FOUND clause.

READ ...
  ...
  FIND ...
...
END-ALL
SORT ...
  DISPLAY ...   
END-SORT
...

2nd Phase - Sorting the Records

The records are sorted.

3rd Phase -Processing the Sorted Records

The statements after the SORT statement are executed for all records on the intermediate storage in the specified sorting sequence. Database fields to be referenced after a SORT statement must be correctly referenced using the appropriate statement label or reference number.

Top of page

Example

Example 1 - SORT

** Example 'SRTEX1S': SORT (structured mode)                            
************************************************************************
DEFINE DATA LOCAL                                                       
1 EMPL-VIEW VIEW OF EMPLOYEES                                           
  2 CITY                                                                
  2 SALARY      (1:2)                                                   
  2 PERSONNEL-ID                                                        
  2 CURR-CODE   (1:2)                                                   
*                                                                       
1 #AVG          (P11)                                                   
1 #TOTAL-TOTAL  (P11)                                                   
1 #TOTAL-SALARY (P11)                                                   
1 #AVER-PERCENT (N3.2)                                                  
END-DEFINE                                                              
*                                                                       
LIMIT 3                                                                 
FIND EMPL-VIEW WITH CITY = 'BOSTON'                                     
  COMPUTE #TOTAL-SALARY = SALARY (1) + SALARY (2)                       
  ACCEPT IF #TOTAL-SALARY GT 0                                          
  /*                                                           
END-ALL                                                        
AND                                                            
SORT BY PERSONNEL-ID USING #TOTAL-SALARY SALARY(*) CURR-CODE(1)
     GIVE AVER(#TOTAL-SALARY)                             
  /*                                                           
  AT START OF DATA                                             
    WRITE NOTITLE '*' (40)                                     
         'AVG CUMULATIVE SALARY:' *AVER (#TOTAL-SALARY) /      
    MOVE *AVER (#TOTAL-SALARY) TO #AVG                         
  END-START                                                    
  COMPUTE ROUNDED #AVER-PERCENT = #TOTAL-SALARY / #AVG * 100   
  ADD #TOTAL-SALARY TO #TOTAL-TOTAL                            
  /*                                                           
  DISPLAY NOTITLE PERSONNEL-ID SALARY (1) SALARY (2)           
          #TOTAL-SALARY CURR-CODE (1)                          
          'PERCENT/OF/AVER' #AVER-PERCENT                      
  AT END OF DATA                                               
    WRITE / '*' (40) 'TOTAL SALARIES PAID: ' #TOTAL-TOTAL      
  END-ENDDATA
END-SORT  
*            
END         

Output of Program SRTEX1S:

PERSONNEL   ANNUAL     ANNUAL   #TOTAL-SALARY CURRENCY PERCENT
     ID       SALARY     SALARY                   CODE     OF
                                                          AVER
  --------- ---------- ---------- ------------- -------- -------
  
  **************************************** AVG CUMULATIVE SALARY:        41900
  
  20007000       16000      15200        31200  USD        74.00
  20019200       18000      17100        35100  USD        83.00
  20020000       30500      28900        59400  USD       141.00
  
  **************************************** TOTAL SALARIES PAID:        125700

The previous example is executed as follows:

First Phase:

Second Phase:

Third Phase:

Equivalent reporting-mode example: SRTEX1R.

Example 2 - SORT

** Example 'SRTEX2': SORT                                               
************************************************************************
DEFINE DATA LOCAL                                                       
1 VEHIC-VIEW VIEW OF VEHICLES                                           
  2 MAKE                                                                
  2 YEAR                                                                
END-DEFINE                                                              
*                                                                       
LIMIT 10                                                                
*                                                                       
READ VEHIC-VIEW                                                         
END-ALL                                                                 
SORT BY MAKE YEAR USING KEY                                       
  DISPLAY NOTITLE (AL=15) MAKE (IS=ON) YEAR                             
  AT BREAK OF MAKE                                                      
    WRITE '-' (20)                                                      
  END-BREAK                                                             
END-SORT                                                           
END  

Output of Program SRTEX2S:

     MAKE       YEAR 
--------------- -----
                     
FIAT             1980
                 1982
                 1984
-------------------- 
PEUGEOT          1980
                 1982
                 1985
-------------------- 
RENAULT          1980
                 1980
                 1982
                 1982
--------------------

Top of page

Using External Sort Programs

In Natural, sort operations are by default processed by Natural's internal sort program, as described above. However, an external sort program can be used. This external sort program then processes the sort operations instead of Natural's internal sort program.

The external sort program to be used is determined by the Natural administrator in the macro NTSORT of the Natural parameter module. For the use of an external sort program, additional JCL is required. Ask your Natural administrator for information.

Top of page