GUIDELINE 6.33
Sorted Filling
ABAP_BACKGROUND
The statement APPEND for attaching rows to an internal table has
the addition SORTED BY . This addition can be used to fill a
standard table by using sorted filling. The prerequisites are:
A value greater than zero must be specified for the addition
INITIAL SIZE .
Only the APPEND statement with the SORTED BY addition can
be used to fill the internal table.
Once these prerequisites have been met, a ranking list is generated that
contains at most the same number of rows as specified with
INITIAL
SIZE . This list is sorted by the component specified after SORTED
BY in descending order.
ABAP_RULE
Create ranking lists with unsorted filling
Do not use the SORTED BY addition of the APPEND statement
to generate ranking lists. Use the SORT statement instead.
ABAP_DETAILS
If the APPEND statement is used with the SORTED BY
addition, you cannot simply append rows (contrary to what the name
implies). Instead, a complicated process is started that only generates
a ranking list if specific prerequisites are met. Otherwise
incomprehensible results are returned. In addition, it is not possible
to sort by more than one column.
The SORT statement is more robust, more powerful and easier to
comprehend when used in this scenario.
Bad Example
The following source code shows how to create a ranking list of the ten
longest distances from a table of flight connections, by using the rule
APPEND SORTED BY . This rule is no longer recommended, as
described above. When declaring the ranking list table, you need to
specify the addition INITIAL SIZE .
...
DATA distance_list TYPE TABLE OF spfli-distance
INITIAL SIZE 10.
FIELD-SYMBOLS <(><<)>spfli_wa> LIKE LINE OF spfli_tab.
...
LOOP AT spfli_tab ASSIGNING <(><<)>spfli_wa>.
APPEND <(><<)>spfli_wa>-distance TO distance_list
SORTED BY table_line.
ENDLOOP.
...
Good Example
The following source code shows how to create the same ranking list in
the previous example, but using the more robust statement SORT .
...
DATA distance_list TYPE TABLE OF spfli-distance.
FIELD-SYMBOLS <(><<)>spfli_wa> LIKE LINE OF spfli_tab.
...
SORT spfli_tab BY distance DESCENDING.
LOOP AT spfli_tab TO 10 ASSIGNING <(><<)>spfli_wa>.
APPEND <(><<)>spfli_wa>-distance TO distance_list.
ENDLOOP.
...
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved