Entering content frameSorting Internal Tables 

You can sort a standard or hashed table in a program. To sort a table by its key, use the statement

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE].

The statement sorts the internal table <itab> in ascending order by its key. The statement always applies to the table itself, not to the header line. The sort order depends on the sequence of the standard key fields in the internal table. The default key is made up of the non-numeric fields of the table line in the order in which they occur.

You can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending.

The larger the sort key, the more time the system needs to sort the table. If the sort key contains an internal table, the sorting process may be slowed down considerably.

You cannot sort a sorted table using the SORT statement. The system always maintains these tables automatically by their sort order. If an internal table is statically recognizable as a sorted table, the SORT statement causes a syntax error. If the table is a generic sorted table, the SORT statement causes a runtime error if the sort key is not the same as an extract of the beginning of the table key, you sort in descending order, or use the AS TEXT addition. In other words, the SORT statement is only allowed for generic internal tables, if it does not violate the internal sort order.

Sorting by Another Sort Key

If you have an internal table with a structured line type that you want sort by a different key, you can specify the key in the SORT statement:

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]
  BY <f1> [ASCENDING|DESCENDING] [AS TEXT]
 ...
 <fn> [ASCENDING|DESCENDING] [AS TEXT].

The table is now sorted by the specified components <f

1 > ... <f n > instead of by the table key. The number of sort fields is limited to 250. The sort order depends on the sequence of the fields <f i>. The sort sequence specified before BY applies to all fields. The sort sequence after a field applies only to that column of the table.

You can specify a sort field dynamically by specifying (<f>) instead of <f

i >. The contents of the field <f> determines the name of the sort field. If <f> is empty when the statement is executed, the field is ignored in the sort. If it contains an invalid component name, a runtime error occurs.

Sorting alphabetically

As well as the ASCENDING or DESCENDING addition, you can also specify that the entire sort or each sort field should be alphabetical.

SORT <itab> ... AS TEXT ... .

This addition affects the sort method for strings. Without the addition, strings are sorted according to the sequence specified by the hardware platform. With the option AS TEXT, the system sorts character fields alphabetically according to the current text environment. By default, the text environment is set in the user master record. However, you can also set it specifically using the statement

SET LOCALE LANGUAGE

The AS TEXT addition saves you having to

convert strings into a sortable format. Such a conversion is only necessary if you want to
  • Sort an internal table alphabetically and then use a binary search
  • resort an internal table with character fields as its key several times, since only one conversion is then required
  • Construct an alphabetical index for database tables in your program

If the AS TEXT addition is applied to the entire sort, it only affects sort fields with type C. If you apply the AS TEXT addition to a single sort field, it must have type C.

Stable sort

The option

SORT <itab> ... STABLE.

allows you to perform a stable sort, that is, the relative sequence of lines that are unchanged by the sort is not changed. If you do not use the STABLE option, the sort sequence is not preserved. If you sort a table several times by the same key, the sequence of the table entries will change in each sort. However, a stable sort takes longer than an unstable sort.

Examples

Example

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.

Example

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.

 

 

 

Leaving content frame