Entering content frame

Sorting Internal Tables Locate the document in its SAP Library structure

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 order.

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 textaddition. 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 f1 ... fn 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 f1 ... fn . 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 f1 ... fn. 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.

If the line type of the internal table contains object reference variables as component <comp> or if the entire line type is a reference variable, the attributes of the <attr> object to which the respective line reference points can be specified as key values using <comp->attr or table_line->attr>.

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’s master record. However, you can also set it specifically using the statement

SET LOCALE LANGUAGE

. The AS textaddition 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 textaddition 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.

Example

REPORT demo_int_tables_sort_stable.

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 list 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

REPORT demo_int_tables_sort_text.
DATA: BEGIN OF line,
        text(6) TYPE c,
        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 itabcontains 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 xtexthas the same result as the alphabetical sorting by the field text.

 

 

Leaving content frame