Sort Catalog in ALV grid display


If we need ALV grid / LIST output in which certain fields are sorted and grouped by default without user action.

Solution: We can build a sort catalog to do the same.

Data Declarations required:

wa_sort         TYPE    slis_sortinfo_alv,
it_sortcat      TYPE    TABLE OF slis_sortinfo_alv,

Assignment / Populating :

Use a subroutine to populate sort catalog . Call this subroutine before ALV grid display. spos refers to the position/order of sort and fieldname refers to the column name. For example to sort by WERKS and MATNR I am using. Don’t forget to clear the internal table at first.

FORM f_build_sortcat .

  CLEAR it_sortcat[].

  wa_sort-spos      = 1. "primary sort

  wa_sort-fieldname = 'WERKS'.

  APPEND wa_sort TO it_sortcat.

  wa_sort-spos      = 2. "secondary sort

  wa_sort-fieldname = 'MATNR'.

  APPEND wa_sort TO it_sortcat.

ENDFORM.                    " build_sortcat

Usage :

 PERFORM f_build_sortcat. "build sort catelog

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "call function for alv grid disply
       i_callback_program                =  sy-cprog
       it_fieldcat                       =  it_fieldcat[]
      it_sort                           =  it_sortcat[]
       is_layout                         =  wa_fieldlayout
       i_callback_top_of_page            =  lv_top_of_page
       i_callback_user_command           =  lv_callback


        t_outtab                          = it_final


       program_error                     = 1
       OTHERS                            = 2

Call the subroutine before calling FM. Pass the sort internal table to it_sort in Exporting parameters.

Info: Make sure you write the fieldnames WERKS etc in capitals or else dump may occur due to mismatch of fieldnames.