Sorting Internal Tables in ABAP - Examples
Details
- Details
- Category: ABAP
- Created on Thursday, 15 November 2012 08:37
- Last Updated on Thursday, 15 November 2012 08:43
- Published on Thursday, 15 November 2012 08:37
- Written by Administrator
- Hits: 32682
Examples
DATA: BEGIN OF LINE,
LAND(3) TYPE C,
NAME(10) TYPE C,
AGE TYPE I,
WEIGHT TYPE P DECIMALS 2,
END OF LINE.
DATA ITAB LIKE STANDARD TABLE OF LINE WITH NON-UNIQUE KEY LAND.
LINE-LAND = 'G'. LINE-NAME = 'Hans'.
LINE-AGE = 20. LINE-WEIGHT = '80.00'.
APPEND LINE TO ITAB.
LINE-LAND = 'USA'. LINE-NAME = 'Nancy'.
LINE-AGE = 35. LINE-WEIGHT = '45.00'.
APPEND LINE TO ITAB.
LINE-LAND = 'USA'. LINE-NAME = 'Howard'.
LINE-AGE = 40. LINE-WEIGHT = '95.00'.
APPEND LINE TO ITAB.
LINE-LAND = 'GB'. LINE-NAME = 'Jenny'.
LINE-AGE = 18. LINE-WEIGHT = '50.00'.
APPEND LINE TO ITAB.
LINE-LAND = 'F'. LINE-NAME = 'Michele'.
LINE-AGE = 30. LINE-WEIGHT = '60.00'.
APPEND LINE TO ITAB.
LINE-LAND = 'G'. LINE-NAME = 'Karl'.
LINE-AGE = 60. LINE-WEIGHT = '75.00'.
APPEND LINE TO ITAB.
PERFORM LOOP_AT_ITAB.
SORT ITAB.
PERFORM LOOP_AT_ITAB.
SORT ITAB.
PERFORM LOOP_AT_ITAB.
SORT ITAB STABLE.
PERFORM LOOP_AT_ITAB.
SORT ITAB DESCENDING BY LAND WEIGHT ASCENDING.
PERFORM LOOP_AT_ITAB.
FORM LOOP_AT_ITAB.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-LAND, LINE-NAME, LINE-AGE, LINE-WEIGHT.
ENDLOOP.
SKIP.
ENDFORM.
The output is:
G Hans 20 80.00
USA Nancy 35 45.00
USA Howard 40 95.00
GB Jenny 18 50.00
F Michele 30 60.00
G Karl 60 75.00
F Michele 30 60.00
G Hans 20 80.00
G Karl 60 75.00
GB Jenny 18 50.00
USA Howard 40 95.00
USA Nancy 35 45.00
F Michele 30 60.00
G Karl 60 75.00
G Hans 20 80.00
GB Jenny 18 50.00
USA Howard 40 95.00
USA Nancy 35 45.00
F Michele 30 60.00
G Karl 60 75.00
G Hans 20 80.00
GB Jenny 18 50.00
USA Howard 40 95.00
USA Nancy 35 45.00
USA Nancy 35 45.00
USA Howard 40 95.00
GB Jenny 18 50.00
G Karl 60 75.00
G Hans 20 80.00
F Michele 30 60.00
The program sorts a standard table with one key field four times. First, the table is sorted twice by the key field (LAND) without the STABLE addition. The sort is unstable. The sequence of the second and third lines changes. The same sort is then performed using the STABLE addition. The sort is stable. The lines remain in the same sequence. Then, it is sorted by a sort key defined as LAND and WEIGHT. The general sort order is defined as descending, but for WEIGHT it is defined as ascending.
DATA: BEGIN OF LINE,
TEXT(6),
XTEXT(160) TYPE X,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY TEXT.
LINE-TEXT = 'Muller'.
CONVERT TEXT LINE-TEXT INTO SORTABLE CODE LINE-XTEXT.
INSERT LINE INTO TABLE ITAB.
LINE-TEXT = 'Möller'.
CONVERT TEXT LINE-TEXT INTO SORTABLE CODE LINE-XTEXT.
INSERT LINE INTO TABLE ITAB.
LINE-TEXT = 'Moller'.
CONVERT TEXT LINE-TEXT INTO SORTABLE CODE LINE-XTEXT.
INSERT LINE INTO TABLE ITAB.
LINE-TEXT = 'Miller'.
CONVERT TEXT LINE-TEXT INTO SORTABLE CODE LINE-XTEXT.
INSERT LINE INTO TABLE ITAB.
SORT ITAB.
PERFORM LOOP_AT_ITAB.
SORT ITAB BY XTEXT.
PERFORM LOOP_AT_ITAB.
SORT ITAB AS TEXT.
PERFORM LOOP_AT_ITAB.
FORM LOOP_AT_ITAB.
LOOP AT ITAB INTO LINE.
WRITE / LINE-TEXT.
ENDLOOP.
SKIP.
ENDFORM.
This example demonstrates alphabetical sorting of character fields. The internal table ITAB contains a column with character fields and a column with corresponding binary codes that are alphabetically sortable. The binary codes are created with the CONVERT statement (see Converting to a Sortable Format). The table is sorted three times. First, it is sorted binarily by the TEXT field. Second, it is sorted binarily by the XTEXT field. Third, it is sorted alphabetically by the TEXT field. Since there is no directly corresponding case in English, we have taken the results from a German text environment:
Miller
Moller
Muller
Möller
Miller
Moller
Möller
Muller
Miller
Moller
Möller
Muller
After the first sorting, 'Möller' follows behind 'Muller' since the internal code for the letter 'ö' comes after the code for 'u'. The other two sorts are alphabetical. The binary sort by XTEXT has the same result as the alphabetical sorting by the field TEXT.
Article from http://help.sap.com
- << Prev
- Next