SORT itab

SORT - Sorting an internal table

Additions:

1. ... BY f1 f2 ... fn
2. ... ASCENDING
3. ... DESCENDING
4. ... AS TEXT


Effect

The entries in the
internal table are sorted in ascending order using the key from the table definition ( DATA, TYPES).

Effect

Uses the sort key defined by the sub-fields f1, f2, ..., fn of the table itab instead of the table key. The fields can be of any type; even number fields and tables are allowed.

You can also specify the sort fields dynamically in the form (name). If name is blank at run time, the sort field is ignored. If itab is a table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. A field symbol that is not assigned is ignored. If a field symbol is assigned, but does not point to the header line of the internal table, a runtime error occurs.

If you use one of the additions 2 to 4 before BY, it applies to all fields of the sort key by default. You can also specify these additions after each individual sort field f1, f2, ..., fn. For each key field, this defines an individual sort rule which overrides the default.

Effect

Sorts in ascending order. This is also the default if no sort order is specified directly after SORT. For this reason, it is not necessary to specify ASCENDING explicitly as the default sort order.

With the addition BY, you can also specify ASCENDING directly after a sort field to define ascending order explicitly as the sort sequence for this field.

Effect

Sorts in descending order. If the addition comes right after SORT, DESCENDING is taken as the default for all fields of the sort key.

With the addition BY, you can also specify DESCENDING directly after a sort field.

Effect

Text fields are sorted appropriate to the locale. This means that the relative order of characters is defined according to the text environment being used.

When an internal mode is opened (in other words, when a roll area is opened), the text environment is automatically set to the logon language specified in the user master record. If necessary, however, you can change the text environment explicitly in your program by using a SET-LOCALE statement.

If the addition comes directly after itab, locale-specific rules are used for all fields of the sort key where the type of these fields is C or W. After the sort, the sequence of entries usually does not match the sequence which results otherwise, without using the addition AS TEXT , i.e. with binary sorting.

With the addition BY, you can also specify AS TEXT directly after a sort field, provided it is of type C or W, or a structured type. Otherwise, a runtime error occurs. In sort fields with a structured type, AS TEXT only affects subcomponents with type C or W.

DATA: BEGIN OF PERSON OCCURS 5, 
		NAME(10)   TYPE C, 
		AGE		TYPE I, 
		COUNTRY(3) TYPE C, 
	END OF PERSON. 
 
PERSON-NAME	= 'Muller'. PERSON-AGE = 22. 
PERSON-COUNTRY = 'USA'.	APPEND PERSON. 
PERSON-NAME	= 'Moller'. PERSON-AGE = 25. 
PERSON-COUNTRY = 'FRG'.	APPEND PERSON. 
PERSON-NAME	= 'Möller'. PERSON-AGE = 22. 
PERSON-COUNTRY = 'USA'.	APPEND PERSON. 
PERSON-NAME	= 'Miller'. PERSON-AGE = 23. 
PERSON-COUNTRY = 'USA'.	APPEND PERSON. 
 
SORT PERSON.

Now, the sequence of the table entries is as follows:

Miller 23 USA
Moller 25 FRG
Muller 22 USA
Möller 22 USA

If, for example, you apply German sort rules where the umlaut comes directly after the letter 'o' in the sort, the data record beginning with 'Möller' would not be in the right place in this sequence. It should come second.

Provided a German-language locale is set (e.g. sorting is according to the rules of Duden, see also SET LOCALE ), you can sort the names according to German rules as follows:

SORT PERSON BY NAME AS TEXT. 

Now, the sequence of table entries is as follows:

Miller 23 USA
Moller 25 FRG
Möller 22 USA
Muller 22 USA

Further examples:

SORT PERSON DESCENDING BY COUNTRY AGE NAME. 

Now, the sequence of table entries is as follows:

Miller 23 USA
Möller 22 USA
Muller 22 USA
Moller 25 FRG

SORT PERSON DESCENDING BY AGE ASCENDING NAME AS TEXT. 

Now, the sequence of table entries is as follows:

Muller 22 USA
Möller 22 USA
Miller 23 USA
Moller 25 FRG

Related

APPEND ... SORTED BY , SORT for extracts