SAP SORT ITAB ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID SORT-ITAB
• SORT itab ABAP Statement

SORT itab
Short Reference

ABAP_SYNTAX
SORT itab $[STABLE$]
${ ${ $[ASCENDING$|DESCENDING$]
$[AS TEXT$]
$[BY ${comp1 $[ASCENDING$|DESCENDING$] $[AS TEXT$]$}
${comp2 $[ASCENDING$|DESCENDING$] $[AS TEXT$]$}
... $] $}
$| ${ $[BY (otab)$] $}
$| ${ $[BY expr$] $} $}.

ABAP Addition
1 ... STABLE
2 ... ASCENDING$|DESCENDING
3 ... AS TEXT
4 ... BY compi $[ASCENDING$|DESCENDING$] $[AS TEXT$]
5 ... BY (otab)
6 ... BY expr

What does it do?
This statement sorts an internal table itab according to the content of its components. Here, size comparisons are performed by default using the general comparison rules, that is:
Numeric and byte-like components are sorted by their values.
Character-like components are sorted by default by their binary representation (code page). Textual sorting of character-like components can be performed using the addition AS TEXT.
The sizes of other component types are compared using the corresponding rules for reference variables, structures, and internal tables.
If no explicit sort key is specified using the addition BY, the internal table itab is sorted by the primary table key. The priority of the sort depends on the order in which the key fields 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. If the primary table key of a standard table is empty, no sort takes place. If this is known statically, the syntax check produces a warning.
Sorting is unstable by default, which means that the relative order of lines that do not differ in sort keys is not preserved when they are sorted. The order can be different depending on the platform or when sorted multiple times. The addition STABLE can be used for stable sorting.
itab expects a standard table or a hashed table.
In standard tables, the primary table index is applied in accordance with the sort order
In hashed tables, the internal order is modified. This internal order was defined either by inserting lines into the internal table or by a previous sort using the statement SORT.
In both table categories, SORT specifies the order in which a subsequent LOOP runs without the addition USING KEY.
Sorted tables cannot be sorted using SORT and applying the statement SORT to sorted tables is forbidden by the syntax. If it is not determined until runtime that a sorted table is to be sorted, an uncatchable exception is raised if this action could modify the existing sorting. The latter occurs in the following cases:
if the addition BY is used to specify a different sort key as the initial part of the table key.
if the addition DESCENDING is used.
if the addition AS TEXT is used.
if an attribute of an object is specified as a component in the addition BY.
Otherwise, the statement SORT is ignored for sorted tables.



Latest notes:

It is best to specify an explicit sort key behind BY, if possible. An implicit sort behind the primary table key, which can itself, in standard tables, be defined implicitly as a standard key, makes a program difficult to understand and possibly unpredictable.
When using the primary table key, it should be noted that this key can be the standard key , which can also have unexpected consequences:
If the line type is structured, the table is sorted by all character-like and byte-like components.
The standard key of a standard table can be empty.
Secondary table keys cannot be specified as sort keys.
Sorting using SORT does not affect the assignment of lines to a secondary table index .
It is possible to sort columns with reference types but doing this is questionable. Here, it is important to note that no comparison rule is defined for non-initial invalid references. An internal table can only be sorted by valid or initial references. A non-initial, invalid reference leads to a runtime error if it is involved in sorting.
The addition GROUP BY of the statement LOOP AT itab or of a FOR expression also has the additions ASCENDING and DESCENDING for sorting groups. These can be used as an extension to the statement SORT if its sort criteria are not sufficient.
NON_V5_HINTS
For the latter see the executable example.
The system class CL_ABAP_ITAB_UTILITIES contains method VIRTUAL_SORT, which can be used to virtually sort a set of internal tables. See also the executable examples listed below.
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Simplest form of the statement SORT for internal tables. The hashed table carriers is sorted by its primary key, that is by the column carrid.
ABEXA 00674
ABAP_EXAMPLE_END

ABAP_EXAMPLES_ABEXA
Sorting Internal Tables
Sorting Internal Tables with Secondary Keys
ABAP_EXAMPLE_END
• STABLE SORT itab

ABAP Addition

What does it do?
STABLE is used to achieve stable sorting, which means that the relative order of lines that do not differ in the sort key remains unchanged in the sort. If the addition STABLE is not specified, the order is not stable:
The order can depend on the platform.
Multiple sorting of a table using the same sort key can produce a different order each time the table is sorted.

ABAP_EXAMPLE_VX5
Stable sorting of the internal table flights by columns cityfrom cityto, whereby the order within this sorting with respect to carrid and connid is preserved.
ABEXA 00675
ABAP_EXAMPLE_END
• ASCENDING SORT itab
• DESCENDING SORT itab

ABAP Addition

What does it do?
The addition ASCENDING or DESCENDING can be used to specify the sort direction explicitly as ascending or descending. If neither of the additions is specified, the table is sorted in ascending order. This sort direction can be overwritten after the addition BY for components listed individually here.

ABAP_EXAMPLE_VX5
The internal table itab is sorted by its primary key in descending order, that is, by its lines. Next, LOOP AT GROUP BY can be used for grouping and determine the number of lines per group.
ABEXA 00676
ABAP_EXAMPLE_END
• AS TEXT SORT itab

ABAP Addition

What does it do?
The addition AS TEXT specifies that text-like components are sorted in accordance with the locale of the current text environment. If AS TEXT is not specified, text-like components are sorted according to the encoding in the code page of the current text environment. This can be overwritten after the addition BY for the components listed individually here. The text environment is set when an ABAP_ISESS is opened or by using the statement SET LOCALE.



Latest notes:

The result of a sorting without the addition AS TEXT depends on the operating system of the host computer of the current ABAP_ASINSTANCE . Although the sequence of individual letters that belong to the activated language remains the same across different operating systems, there are differences in terms of the characters that do not belong to the alphabet of the activated language. Even if only the letters from the alphabet of the activated language are used, some slight differences occur when sorting complete words. Furthermore, the order of uppercase and lowercase letters is specific to the operating system.
The use of the addition AS TEXT usually makes the statement CONVERT TEXT superfluous in the context of internal tables.
A sort without the addition AS TEXT is considerably faster than a sort with this addition. If it is certain that both sorts produce the same order, the addition AS TEXT is not necessary. The latter can be the case if, for example, text-like components contain characters from the ASCII character set only and only lowercase or uppercase letters.
BEGIN_SECTION SAP_INTERNAL_HINT
If the profile parameter abap/set_textenv/skip_all has a value other than 0, the addition AS TEXT does not work.
END_SECTION SAP_INTERNAL_HINT
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Sorting of a hashed table text_tab by the order in the code page and in accordance with the locale of the current text environment. If a western European text environment is configured, the sorts produce the orders Miller, Moller, Muller, Möller and Miller, Moller, Möller, Muller respectively (see also the executable example for SET LOCALE).
ABEXA 00677
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Sorting Internal Tables Alphabetically
ABAP_EXAMPLE_END
• BY SORT itab
• ASCENDING SORT BY - itab
• DESCENDING SORT BY - itab
• AS TEXT SORT BY - itab

ABAP Addition TEXT$]

What does it do?
The addition BY compi does not sort the table by the primary table key, but by the components comp1 comp2... specified after it. The components are specified as described under Specifying Components. If all components are specified using name variables and these contain only blanks, 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. The specified components can also be duplicated or overlapping. The specified components can have any data type. The relevant comparison rules apply to the evaluation.
If neither of the additions ASCENDING or DESCENDING are specified after compi, the sort direction specified by addition 2 is used. If one of the additions ASCENDING or DESCENDING is specified, it overwrites the default for this component.
If the addition AS TEXT is not specified after a text-like component compi, the default specified by addition 3 is used. If the addition AS TEXT is specified after a text-like component, it overwrites the default for this component. In non-text-like components, AS TEXT cannot be specified, unless a structured component is specified. In structured components, AS TEXT only affects text-like components.



Latest notes:

If the line type of the internal table is not known statically, the components can only be specified dynamically and not directly.
Instead of individual dynamic components, an internal table can be specified directly as otab or as the result of an expression expr as a dynamic sort key (see additions 5 and 6). Using a table like this has the advantage that any exceptions are catchable. When specifying the table, the number of components of the sort key is also dynamic. In contrast, when individual dynamic components are used, a character-like data object must be specified for any required component, which is ignored if it only contains blank characters.
NON_V5_HINTS
An obsolete variant allows field symbols to also be specified for the components outside of classes, for standard tables.
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Sorting of the internal table itab in ascending order by column col1 and in descending order by column col2.
ABEXA 00678
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition BY (otab) does not sort the table by the primary table key, but by the component specified dynamically in the internal table otab. Each line of the table otab defines a component of the sort key. The priority of the sort is based on the order of the lines in otab. 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:
NAME of the type SSTRING for specifying a component of the sort key. The component is specified in the form comp_name[+off(len)], where comp_name must be the name of a component in itab in uppercase letters. The component name may contain offsets and lengths , structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
DESCENDING of the type CHAR with length 1 for specifying the sort direction for the current component. If DESCENDING is initial, the sort is performed in ascending order. If DESCENDING has the value X, the table is sorted in descending order.
ASTEXT of the type CHAR with length 1 for the text sorting of the current component. If ASTEXT has the value X , the sort is performed as with the addition AS TEXT. This is only possible for character-like components. If ASTEXT is initial, character-like components are sorted in accordance with their binary representation.
If a column of otab has invalid content, that is, if NAME contains the name of a component that does not exist or an incorrect offset/length or if DESCENDING and ASTEXT do not contain X or the initial value, a catchable exception of the class CX_SY_DYN_TABLE_ILL_COMP_VAL is raised.



Latest notes:

The addition BY (otab) cannot be combined with BY compi.
When using the addition BY (otab), it is not possible to use DESCENDING or AS TEXT to specify a descending sort direction or textual sorting for all components.
If a single parenthesized data object (dobj) is specified after the addition BY, its data type decides whether its content is used to specify a single dynamic component or multiple components. In either case, no sort takes place if dobj is initial.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX
Reading of a database table into a dynamic internal table dynamically and sorting its content dynamically. The name of the database table and the names of the columns by which the table sorted can be entered.
ABEXA 00679
ABAP_EXAMPLE_END

ABAP_EXAMPLE_V5
Reading of a database table into a dynamic internal table dynamically and sorting its content dynamically. The name of the database table and the names of the columns by which the table sorted can be entered.
ABEXA 01690
ABAP_EXAMPLE_END

ABAP Addition

What does it do?
The addition BY expr can be used to specify an expression or a functional method call expr whose result is an internal table with the same type and content as in the preceding addition BY (otab). expr is a general expression position. The behavior is the same as when specifying a parenthesized internal table directly.



Latest notes:

Parentheses cannot be placed around expr.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The above example for specifying BY (otab) can be written shorter as shown below. Instead of specifying the unnecessary internal table order, it is possible to directly specify the tabular value constructed using the value operator VALUE of the required content.
ABEXA 01691
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Sorting Internal Tables Dynamically.
ABAP_EXAMPLE_END