ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab → LOOP AT itab - GROUP BY → Examples of Grouping with LOOP
Internal Tables, Grouping with LOOP and Sort
This example demonstrates how groupings are used for sorts.
Other versions:
7.31 | 7.40 | 7.54
Source Code
TYPES itab TYPE STANDARD TABLE OF string WITH EMPTY KEY.
DATA(itab) =
VALUE itab( ( `d` ) ( `B` ) ( `D` ) ( `b` ) ( `a` )
( `D` ) ( `a` ) ( `C` ) ( `A` ) ( `c` ) ).
cl_demo_output=>write( itab ).
DATA jtab TYPE itab.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>)
GROUP BY to_upper( <line> ) ASCENDING
ASSIGNING FIELD-SYMBOL(<grp1>).
LOOP AT GROUP <grp1> ASSIGNING FIELD-SYMBOL(<mbr1>)
GROUP BY <mbr1> DESCENDING
ASSIGNING FIELD-SYMBOL(<grp2>).
jtab = VALUE #( BASE jtab
FOR <mbr2> IN GROUP <grp2> ( <mbr2> ) ).
ENDLOOP.
ENDLOOP.
cl_demo_output=>display( jtab ).
Description
In this example, the additions ASCENDING
and DESCENDING
of GROUP BY
are used to sort a character-like table while ignoring case and then sort within the groups to split uppercase and lowercase. A further
executable example shows how the LOOP
s can be replaced by a single expression.