SORT ( SAP ABAP Keyword)

SORT is a keyword used in SAP ABAP programming.This tutorial covers its introduction & syntax details.

SORT

Variants

1. SORT itab.
2. SORT.

Variant 1
SORT itab.

Additions

1. … DESCENDING
2. … ASCENDING
3. … BY f1 f2 … fi

Effect
Sorts the entries of the internal table itab in ascending order.

The default key is used as the sort key for internal tables.

Notes
The number of sort fields is restricted to 250.
The
sorting process is not stable, i.e. if no sort is performed for a
predefined sequence of fields, the sequence is not retained.
To delete all duplicate entries from a sorted internal table, you can specify DELETE ADJACENT DUPLICATES FROM itab after SORT .
The
sort itself uses the Quicksort process where the key fields for all the
data records are retrieved and placed in an area of main memory.
If
there is not enough space in memory, the key fields are written to a
temporary file and sorted by an external sort program. You can modify
the directory which the SORT uses to store such auxiliary files by
modifying the SAP profile parameter DIR_SORTTMP . Normally, auxiliary
files are created in the SAP data directory (SAP profile parameter
DIR_DATA ).

Addition 1
… DESCENDING

Effect
Sorts itab in descending order.

Addition 2
… ASCENDING

Effect
Sorts itab in ascending order (default).

Addition 3
… BY f1 f2 … fi

Effect
Sorts
itab by the sub-fields f1 , f2 , …, fi which form the sort key. These
fields can be any type (even number fields or tables). Unless you
specify otherwise, the sort is in ascending order. You can also use
additions 1 and 2 before BY if you want all sub-fields to apply. To
change the sort sequence for each individual field, specify DESCENDING
or ASCENDING after each of the sub-fields f1 , f2 , …, fi .

Example

DATA: BEGIN OF PEOPLE OCCURS 5,
NAME(10),
AGE TYPE I,
NATIONALITY(3),
END OF PEOPLE.
PEOPLE-NAME = ‘Sally’. PEOPLE-AGE = 22.
PEOPLE-NATIONALITY = ‘USA’. APPEND PEOPLE.
PEOPLE-NAME = ‘Peter’. PEOPLE-AGE = 25.
PEOPLE-NATIONALITY = ‘FRG’. APPEND PEOPLE.
PEOPLE-NAME = ‘Paula’. PEOPLE-AGE = 22.
PEOPLE-NATIONALITY = ‘USA’. APPEND PEOPLE.
PEOPLE-NAME = ‘Peter’. PEOPLE-AGE = 23.
PEOPLE-NATIONALITY = ‘USA’. APPEND PEOPLE.
SORT PEOPLE.

The sequence of table entries now reads: ‘Paula’ , ‘Peter’ from ‘FRG’ , ‘Peter’ from ‘USA’ , ‘Sally’ .

SORT PEOPLE DESCENDING BY NATIONALITY AGE NAME.

The sequence now reads: ‘Peter’ from ‘USA’ , ‘Sally’ , ‘Paula’ , ‘Peter’ from ‘FRG’ .

SORT PEOPLE DESCENDING BY AGE ASCENDING NAME.

The sequence now reads: ‘Sally’ , ‘Paula’ , ‘Peter’ from ‘USA’ , ‘Peter’ from ‘FRG’ .

Notes
If
a sort criterion is not known until runtime, you can use SORT itab BY
… (name) … to specify it dynamically as the contents of the field
name . If name is blank at runtime, the sort criterion is ignored. If
name contains an invalid component name, a runtime error occurs.
You
can use offset and length specifications to further restrict sort
criteria, regardless of whether they are specified statically or
dynamically.
If itab is an internal table with a header line, you
can also use a field symbol pointing to the header line of itab as a
dynamic sort criterion. If the field symbol does not point to the
header line of the internal table, a runtime error occurs.

Note
Performance
The runtime required to sort an internal table increases with the number of entries and the width of the sort key.

Sorting
an internal table with 100 entries and the 50-byte wide default key
would take about 1300 msn (standardized microseconds). A 30-byte wide
sort key would need about 950 msn.
If one of the specified sort criteria is itself an internal table, the SORT may sometimes take longer.
Runtime errors

SORT_ITAB_FIELD_INVALID
:A field symbol used as a dynamic sort criterion does not point to the
header line of the internal table to be sorted.

SORT_TOO_MANY_FIELDS : More than 250 sort criteria.

Related APPEND … SORTED BY

Variant 2
SORT.

Additions

1. … DESCENDING (similar to variant 1)
2. … ASCENDING (similar to variant 1)
3. … BY f1 f2 … fi
4. … BY fg

Effect
Sorts the dataset generated with EXTRACT by the fields in the field group HEADER (see FIELD-GROUPS ).
Here,
blank fields (i.e. fields not defined with EXTRACT ) are inserted
before all non-blank fields, regardless of whether the sort sequence is
in ascending or descending order.

Notes
The number of sort criteria is restricted to 50.
As
with variant 1, any sequence of fields you specify for sorting purposes
does not remain fixed. Any sequence of records which belongs to
different field groups, but has the same HEADER field contents, is
arbitrary.
Again like variant 1, sorting takes place in main memory
if at all possible. If there is insufficient space there, ABAP/4 calls
an external sort program. You can modify the directory used to create
the temporary auxiliary file by modifying the SAP profile parameter
DIR_SORTTMP .
As soon as a dataset has been processed with SORT or LOOP … ENDLOOP , you cannot extract any more records with EXTRACT .

Addition 3
… BY f1 f2 … fi

Effect
Can sort only by fields in the field group HEADER .
Otherwise, the effect is similar to variant 1.

Addition 4
… BY fg

Effect
Sorts by the fields in field group fg .
However,
the only fields which can be sorted are those in the field group HEADER
, i.e. the field group fg can consist only of fields from the field
group HEADER (see INSERT … INTO ).

Example

DATA: ONR(7), DATE TYPE D, POSITION(3) TYPE N,
CUSTOMER(16),
PNR(5) TYPE N, NAME(10), UNITS TYPE I,
ORDERS TYPE I.
FIELD-GROUPS: HEADER, ORDER, PRODUCT, DATE_FIRST.
INSERT ONR DATE POSITION INTO HEADER.
INSERT CUSTOMER INTO ORDER.
INSERT PNR NAME UNITS INTO PRODUCT.
INSERT DATE ONR POSITION INTO DATE_FIRST.
ONR = ‘GF00012’. DATE = ‘19921224’.
POSITION = ‘000’. CUSTOMER = ‘Good friend (2.)’.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = ‘12345’. NAME = ‘Screw’. UNITS = 100.
EXTRACT PRODUCT.
ADD 1 TO POSITION.
PNR = ‘23456’. NAME = ‘Nail’. UNITS = 200.
EXTRACT PRODUCT.
ONR = ‘MM00034’. DATE = ‘19920401’.
POSITION = ‘000’. CUSTOMER = ‘Moneymaker’.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = ‘23456’. NAME = ‘Nail’. UNITS = 300.
EXTRACT PRODUCT.
ADD 1 TO POSITION.
PNR = ‘34567’. NAME = ‘Hammer’. UNITS = 4.
EXTRACT PRODUCT.
ONR = ‘GF00011’. DATE = ‘19921224’.
POSITION = ‘000’. CUSTOMER = ‘Good friend (1.)’.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = ‘34567’. NAME = ‘Hammer’. UNITS = 5.
EXTRACT PRODUCT.
SORT BY DATE_FIRST.
LOOP.
AT ORDER.
WRITE: /, / DATE, ONR, POSITION,
CUSTOMER, ‘ordered:’.
ENDAT.
AT PRODUCT.
WRITE: / DATE, ONR, POSITION,
PNR, NAME, UNITS.
ENDAT.
ENDLOOP.

This generates the following output:

01041992 MM00034 000 Moneymaker ordered:
01041992 MM00034 001 23456 Nail 300
01041992 MM00034 002 34567 Hammer 4

24121992 GF00011 000 Good friend (1.) ordered:
24121992 GF00011 001 34567 Hammer 5

24121992 GF00012 000 Good friend (2.) ordered:
24121992 GF00012 001 12345 Screw 100
24121992 GF00012 002 23456 Nail 200

Note
Performance

The runtime required to sort an internal table increases with the number of entries and the width of the sort key.

Note
Runtime errors

SORT_EXTRACT_TOO_MANY_FIELDS : More than 50 sort criteria

SORT_FIELD_NOT_IN_HEADER : Sort criterion not in field group HEADER

SORT_NO_HEADER : Field group HEADER not created

SORT_WITHIN_LOOP : SORT on extract dataset within LOOP on extract dataset