You can change the sequence of data records according to specific rules by sorting the ALV output. You specify which fields contain the values that are to be sorted alphabetically or numerically, thereby determining the sequence of all rows.
You are able to make the following settings for sorting:
● Create, get, and delete sort settings (sort conditions)
● Setting sort direction and sort sequence
● Grouping same values
● Forbidding sorting for a field
● Enabling sorting by clicking on a column header
● Sorting a field according to the values of another field
● Hide and show tab for sorting (see Providing Standard ALV Functions)
You are able to make calculations using the values of numeric columns (see Calculation (Aggregation)). By default all values of a column are used for this. If you need intermediate results in addition to the overall result of the calculation, the rows that contain the subvalues of an intermediate result need to be next to each other, so you sort the ALV output. If you display intermediate results, these are generated for all sorted columns by default (see Intermediate Results).
Sorting is a property of a field in the ALV output (see Fields). To create, get, or delete the sort condition of a field, use the methods of the interface class IF_SALV_WD_SORT (implementing class CL_SALV_WD_FIELD).
Methods for Creating, Getting, and Deleting the Sort Condition
Function |
Method |
Get sort condition |
GET_SORT_RULE |
Create sort condition |
CREATE_SORT_RULE |
Delete sort condition |
DELETE_SORT_RULE |
The sort condition of a field is represented by an object of the class CL_SALV_WD_SORT_RULE.
If you assign a new data table with a new structure to your ALV output, all sort conditions for all fields are deleted automatically.
For each individual sort condition, you are able to define whether you want to sort the values of the field ascending (for example, a, b, c) or descending (for example, c, b, a): You do this by setting the sort direction.
If you sort the ALV output by multiple fields, the result changes depending on the sequence of fields that is used to sort. By default, the fields are sorted in the sequence in which you generated your sort conditions. You are able to change this sequence. To do this, assign a position number in the sort sequence to the field (or its sort condition).
To change the sort direction or sort sequence of a sort condition, use the methods of the class CL_SALV_WD_SORT_RULE.
Methods for the Sort Direction and Sort Sequence
Function |
Method |
Set sort direction |
SET_SORT_ORDER |
Get sort direction |
GET_SORT_ORDER |
Set position of field within sort sequence |
SET_SORT_POSITION |
Get position of field within sort sequence |
GET_SORT_POSITION |
By default, values that are the same are combined in a sorted ALV output in a sorted field. The values are grouped. You can choose that a value appear in each row, even if this value does not change. You use the methods of the interface class IF_SALV_WD_SORT for this (implementing class CL_SALV_WD_FIELD).
Methods for Grouping Sorted Values
Function |
Method |
Group values/remove grouping |
SET_GROUPING_ALLOWED |
Check whether values are grouped |
IS_GROUPING_ALLOWED |
You can explicitly forbid sorting for a field. This has the following effects:
● The field in question is no longer available on the Sorting tab of the Settings dialog box.
● If you permit the sorting of columns by clicking on column headers (see below), the function is not available for a column for which you have forbidden sorting, and the arrows are hidden.
● If you defined a sort condition for this field in your application, the sorting in the ALV output has no effect.
To forbid sorting for a field, use the methods of the interface class IF_SALV_WD_SORT (implementing class CL_SALV_WD_FIELD).
Methods for Forbidding Sorting
Function |
Method |
Forbid sorting |
SET_SORT_ALLOWED |
Check whether sorting is allowed |
IS_SORT_ALLOWED |
You can display small arrows in the column headers of the ALV output. The user can use these arrows to sort the columns in ascending or descending order. To display these arrows, use the methods of the interface class IF_SALV_WD_STD_FUNCTIONS (implementing class CL_SALV_WD_CONFIG_TABLE).
Methods for Sorting by Clicking on a Column Header
Function |
Method |
Show arrows in column headers |
SET_SORT_HEADERCLICK_ALLOWED |
Check whether arrows are shown in column headers |
IS_SORT_HEADERCLICK_ALLOWED |
You can specify a field according to which the current field is to be sorted if the field itself fails to deliver the required result. You use the methods of the interface class IF_SALV_WD_COLUMN_SERVICE_REF for this (implementing class CL_SALV_WD_COLUMN).
Methods for Sorting According to the Values of Another Field
Function |
Method |
Set field name of other field |
SET_SORT_FIELDNAME |
Get field name of other field |
GET_SORT_FIELDNAME |