SORT itab [STABLE]
{ { [ASCENDING|DESCENDING]
[AS TEXT]
[BY {comp1 [ASCENDING|DESCENDING] [AS TEXT]}
{comp2 [ASCENDING|DESCENDING] [AS TEXT]}
... ] }
| { [BY (otab)] } }.
1. ... STABLE
2. ... ASCENDING|DESCENDING
3. ... AS TEXT
4. ... BY compi [ASCENDING|DESCENDING] [AS TEXT]
5. ... BY (otab)
This statement sorts an internal table itab. As standard, numeric and byte-type components are sorted by their value and character-type components according to their binary representation code page). Textual sorting of character-type components is performed using the addition AS TEXT.
If no explicit sort key is entered using the BY addition, the internal table itab is sorted according to the table key. In this case, the table key can contain a maximum of 250 components. The priority of the sort is based on the order in which the die key field are specified in the table definition. In standard keys, the sort is prioritized according to the order of the key fields in the line type of the table.
For itab, a standard table or a hashed table is expected. Sorted tables cannot be sorted using SORT and the application of the SORT statement on sorted tables is syntactically prohibited. If the system does not notice until runtime that a sorted table is to be sorted, an untreatable exception occurs if this could result in change in the existing sorting. The latter occurs in the following cases:
Otherwise, the SORT statement is ignored for sorted tables.
... STABLE
STABLE is used to perform stable sorting, which means
that the relative sequence of lines that does not change in the sort key remains unchanged in the sort.
Without the STABLE addition, the sequence is not retained
and multiple sorting of a table using the same sort key results in a different sequence each time the table is sorted.
... ASCENDING|DESCENDING
The addition ASCENDING or
DESCENDING can be used to specify the sort direction explicitly as ascending or descending.
If neither of the additions is entered, the table is sorted in ascending order. This sort direction
can be overwritten after the BY addition for components that are individually listed there.
... AS TEXT
The addition AS TEXT specified that text-type components are sorted according to the
locale of the current
text environment. If
AS TEXT is not specified, text-type components are sorted according to the encoding in the
code page of the current text environment. This specification can be overwritten after the
BY addition for the components that are individually listed there. The text environment is
set when an internal session is opened or by using the statement SET LOCALE.
Sorting a hashed table text_tab by alignment in the code page and according to the locale of the current text environment. If a Western European text environment is set, the sorts result in the sequences Miller, Moller, Muller, Möller and Miller, Moller, Möller, Muller respectively. (Also see the example for SET LOCALE).
DATA text_tab TYPE HASHED TABLE OF string
WITH UNIQUE KEY table_line.
INSERT: `Muller` INTO TABLE text_tab,
`Möller` INTO TABLE text_tab,
`Moller` INTO TABLE text_tab,
`Miller` INTO TABLE text_tab.
SORT text_tab.
PERFORM write_text_tab.
SORT text_tab AS TEXT.
PERFORM write_text_tab.
FORM write_text_tab.
FIELD-SYMBOLS <line> TYPE string.
LOOP AT text_tab ASSIGNING <line>.
WRITE / <line>.
ENDLOOP.
SKIP.
ENDFORM.
... BY compi [ASCENDING|DESCENDING] [AS TEXT]
With the addition BY compi, the table is not sorted according to the table key, but instead according to the components comp1 comp2... that are specified after it. The components are specified as described under specifying components, with the restriction that the number of components that can be specified is restricted to 250. In purely dynamic component specification, if all names name only contain blank characters, no sort takes place. The priority of the sort depends on the order in which the components comp1 comp2 ... are specified from left to right.
If neither of the additions ASCENDING or DESCENDING is specified after comp1 comp2 ..., the sort direction specified by addition 2 is used. If one of the additions ASCENDING or DESCENDING is specified, it overwrites the specification for this component.
If the AS TEXT addition is not specified after a text-type component comp1 comp2..., the specification defined by addition 3 is used. If the AS TEXT addition is specified after a text-type component, it overwrites the specification for this component. In non-text-type components, AS TEXT cannot be specified.
In internal tables with header lines,
field symbols can also be entered for comp1 comp2 ...
outside classes. If a component of the header line or the whole header line is assigned to the field
symbol when the statement is executed, the table is sorted according to the corresponding component
or the whole line. If no data object is assigned to a field symbol, the entry is ignored. If a different data object is assigned to a field symbol, an untreatable exception occurs.
... BY (otab)
With the addition BY (otab), the table is not sorted according to the table key, but instead according to the component specified dynamically in the internal table otab (as of release 7.0). Each line of the table otab defined a component of the sort key. The priority of the sort is based on the order of the lines in otab. A maximum of 250 components can be specified. If the table otab is initial, the table is not sorted.
For otab, a standard table of the table type ABAP_SORTORDER_TAB from the ABAP Dictionary must be specified. The line type of this table is the Dictionary structure ABAP_SORTORDER with the following components:
If a column of otab, has invalid content (NAME contains
the name of a component that does not exist or an incorrect offset/length specification, DESCENDING
and ASTEXT do not contain "X" or the initial value), this leads to a treatable exception of the class CX_SY_DYN_TABLE_ILL_COMP_VAL.
Dynamic import of a database table into a dynamic internal table and dynamic sorting of its content. The name of the database table and the names of the columns by which the table is to be sorted can be entered on a selection screen.
PARAMETERS dbtab TYPE c LENGTH 30.
SELECT-OPTIONS columns FOR dbtab NO INTERVALS.
DATA: otab TYPE abap_sortorder_tab,
oline TYPE abap_sortorder,
dref TYPE REF TO data.
FIELD-SYMBOLS: <column> LIKE LINE OF columns,
<itab> TYPE STANDARD TABLE.
TRY.
CREATE DATA dref TYPE STANDARD TABLE OF (dbtab).
ASSIGN dref->* TO <itab>.
CATCH cx_sy_create_data_error.
MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
TRY.
SELECT *
FROM (dbtab)
INTO TABLE <itab>.
CATCH cx_sy_dynamic_osql_semantics.
MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
LOOP AT columns ASSIGNING <column>.
oline-name = <column>-low.
APPEND oline TO otab.
ENDLOOP.
TRY.
SORT <itab> BY (otab).
CATCH cx_sy_dyn_table_ill_comp_val.
MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
Non-Catchable Exceptions
CX_SY_DYN_TABLE_ILL_LINE_TYPE
CX_SY_DYN_TABLE_ILL_COMP_VAL
Non-Catchable Exceptions