Entering content frame

Sorting an Extract Locate the document in its SAP Library structure

You can sort an extract dataset in much the same way as an internal table by using the following statement:

SORT [ASCENDING|DESCENDING] [AS TEXT] [STABLE]
      BY f1 [ASCENDING|DESCENDING] [AS TEXT]
       ...
         fn [ASCENDING|DESCENDING] [AS TEXT].

The SORT statement terminates the creation of the extract dataset of a program and, at the same time, sorts its records. Without the BY option, the system sorts the dataset by the key specified in the header field group.

You can sort an extract dataset as often as you like in a program, using any number of different keys. The only prerequisite is that all fields by which you want to sort are contained in the header during the extraction process. You must not use the SORT statement between LOOP and ENDLOOP. However, you can sort and read the extract dataset in any sequence. After the SORT statement has been executed you can not use any further EXTRACT statements. Otherwise, a runtime error occurs.

You can define a different sort key by using the BYaddition. The system then sorts the dataset according to the specified components f1 ... fn. These components must either be fields of the headerfield group or field groups containing only fields from the header field group. The number of key fields is limited to 50. The sequence of the components f1 ... fn determines the sort order. The system uses the options you specify before BY as a default for all fields specified after BY. The options that you specify after individual fields overwrite for these fields the options specified before BY.

You can define the sort direction using the DESCENDING or ASCENDING additions. The default is ascending order. For character strings, you can use the AS TEXT addition to define the sort method. This forces an alphabetical sort, as with internal tables. If you want to sort an extract dataset alphabetically more than once, you should include a field which can be sorted alphabetically in the sort key instead of the text field for performance reasons. To fill this field, use the CONVERT statement.

If you put AS TEXT before BY, the addition only applies to type c fields in the sort key. If you place AS TEXT after a field, the field must be of type c. If you place AS TEXT after a field group, the option only applies to the type c fields within the group.

This sorting process is not stable, that is, the old sequence of records with the same sort key must not necessarily be kept. To force a stable sort, use the STABLEaddition.

If there is not enough main memory available to sort the data, the system writes data to an external auxiliary file during the sorting process. The name of the file is determined by the SAP profile parameter DIR_SORTTMP.

The SORT statement sorts by all of the fields in the sort key with the contents HEX 00 before all of the other entries. This is significant when you use logical databases. When a logical database has finished reading a hierarchy level, it fills all of the fields at that level with the value HEX 00. Equally, if you use a field list in the GET statement (FIELDS addition), the logical database fills all of the fields not in the field list with HEX 00.

Each sorting process executed on the extract dataset using the SORT statement defines a control level. This is required for subsequent control level processing.

Example

Assume the following program is linked to the logical database F1S.

REPORT demo_extract_sort.

NODES: spfli, sflight.

FIELD-GROUPS: header, flight_info, flight_date.

INSERT: spfli-carrid spfli-connid sflight-fldate
        INTO header,
        spfli-cityfrom spfli-cityto
        INTO flight_info.

START-OF-SELECTION.

GET spfli.
  EXTRACT flight_info.

GET sflight.
  EXTRACT flight_date.

END-OF-SELECTION.

  SORT DESCENDING.

  LOOP.
    AT FIRST.
      WRITE / 'Start of LOOP'.
      ULINE.
    ENDAT.
    AT flight_info WITH flight_date.
      WRITE: / 'Info:',
               spfli-carrid , spfli-connid, sflight-fldate,
               spfli-cityfrom, spfli-cityto.
    ENDAT.
    AT flight_date.
      WRITE: / 'Date:',
                spfli-carrid , spfli-connid, sflight-fldate.
    ENDAT.
    AT LAST.
      ULINE.

      WRITE / 'End of LOOP'.
    ENDAT.
  ENDLOOP.

This example is identical with the example in the section Reading an Extract, apart from the SORT DESCENDING statement. The SORT statement tells the system to sort the extract dataset in descending order by the three fields of the header field group, before reading it using LOOP - ENDLOOP. The end of the list looks like this:

This graphic is explained in the accompanying text

It is worth noting that the records with the value HEX 00 in the field sflight-fldate (undefined characters in the list) are sorted before the remaining records. This is done to preserve the hierarchy of the data from the logical database, independent of the sort sequence.

 

 

 

Leaving content frame