Skip to content

CL_SALV_SORTS

Defines the default sort order for a CL_SALV_TABLE grid.

Purpose

CL_SALV_SORTS lets you pre-sort the ALV grid before it is displayed. Obtain it via lo_alv->get_sorts( ). Call add_sort once per sort criterion — priority follows call order (first call = primary sort).

Key methods

Method Signature Effect
add_sort ( columnname sequence [subtotal] ) Adds a sort criterion; sequence uses IF_SALV_C_SORT constants
delete_sort ( columnname ) Removes a previously added sort criterion
clear_sorts ( ) Removes all sort criteria
set_user_sort ( value = abap_false ) Prevents users from changing the sort interactively

Sort direction constants (IF_SALV_C_SORT)

Constant Value Meaning
if_salv_c_sort=>sort_up A Ascending
if_salv_c_sort=>sort_down D Descending

Example

Sort by user type ascending (primary), then by validity end-date descending (secondary):

REPORT z_salv_sorts_demo.

DATA: lt_users TYPE STANDARD TABLE OF usr02,
      lo_alv   TYPE REF TO cl_salv_table.

START-OF-SELECTION.

  SELECT bname, ustyp, gltgv, gltgb
    FROM usr02
    INTO TABLE @lt_users
    UP TO 100 ROWS.

  TRY.
      cl_salv_table=>factory(
        IMPORTING r_salv_table = lo_alv
        CHANGING  t_table      = lt_users ).

      DATA(lo_sorts) = lo_alv->get_sorts( ).

      " primary sort: user type, ascending
      lo_sorts->add_sort(
        columnname = 'USTYP'
        sequence   = if_salv_c_sort=>sort_up ).

      " secondary sort: validity end-date, descending, with subtotals
      lo_sorts->add_sort(
        columnname = 'GLTGB'
        sequence   = if_salv_c_sort=>sort_down
        subtotal   = abap_true ).

      " lock so the user cannot change the sort order
      lo_sorts->set_user_sort( abap_false ).

      lo_alv->display( ).

    CATCH cx_salv_msg INTO DATA(lx_salv).
      MESSAGE lx_salv->get_text( ) TYPE 'E'.
    CATCH cx_salv_not_found INTO DATA(lx_not_found).
      MESSAGE lx_not_found->get_text( ) TYPE 'E'.
    CATCH cx_salv_existing INTO DATA(lx_exist).
      MESSAGE lx_exist->get_text( ) TYPE 'E'.
  ENDTRY.

Common pitfalls

Default sorts are overridable by the user

Sorts defined here are the initial sort — the user can click column headers to change them unless you call set_user_sort( abap_false ). If a locked sort order is a business requirement, always set this flag.

CX_SALV_NOT_FOUND on missing columns

add_sort raises CX_SALV_NOT_FOUND if the column name does not exist in the table structure. This also happens when the column exists but has been removed from the field catalogue. Catch it explicitly.

CX_SALV_EXISTING on duplicate sort columns

Adding a second sort criterion on the same column raises CX_SALV_EXISTING. Call delete_sort first if you need to re-add with different parameters.

Subtotals require grouping

Setting subtotal = abap_true only makes sense when the column being sorted is a grouping criterion and your ALV display includes aggregations. Without configured aggregations the subtotal parameter has no visible effect.

  • Sort priority is strictly the order of add_sort calls, not the column position in the grid.
  • Sorting happens on the internal table data as displayed — it does not re-execute the SELECT.
  • For very large datasets consider sorting the internal table with SORT before passing it to factory; ALV sorts every time the grid is refreshed.

See also

Comments