Add Default Sorting to ALVgrid report

In order to display an ALV report with specific columns already sorted by default you will need to build a sort catalogue. This is fairly straight forward and is done in the following way:

Step 1. Add data declaration for sort catalogue
Step 2. Add code to build sort catalogue table
Step 3. Update 'REUSE_ALV_GRID_DISPLAY' FM call to include parameter 'it_sort'

Also remember to set at least one number based column to do_sum

fieldcatalog-do_sum = 'X'.


*  ALV data declarations
  data: it_sortcat   type slis_sortinfo_alv occurs 1,
        wa_sort like line of it_sortcat.


 perform build_sortcat.
*&----------------------------------------------------------*
*&      Form  build_sortcat
*&----------------------------------------------------------*
*       Build Sort catalog
*-----------------------------------------------------------*
FORM build_sortcat .
  wa_sort-spos      = 1.
  wa_sort-fieldname = 'EBELN'.
  wa_sort-SUBTOT    = 'X'. "subtotals any totals column by this field
*  gd_sortcat-tabname
  APPEND wa_sort TO it_sortcat.
  wa_sort-spos      = 2.
  wa_sort-fieldname = 'EBELP'.
*  gd_sortcat-tabname
  APPEND wa_sort TO it_sortcat.
ENDFORM.                    " build_sortcat


 call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program      = gd_repid
            i_callback_top_of_page   = 'TOP-OF-PAGE'
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
            it_sort                 = it_sortcat
            i_save                  = 'X'
       tables
            t_outtab                = it_ekko
       exceptions
            program_error           = 1
            others                  = 2.

Related Articles

ALV grid display - Example code and information for creating ALV grids using Function modules
Add User command functionality to ALVgrid report
ALV totals text
Display traffic light icon on ALV grid report
ALV grid display (inc. column total)
SAP ALV consistancy check
Change colour of individual SAP ALV cells within an ALV grid report
SAP ALV row colour change and making each individual row a different color
Make all or individual fields of an ALV grid editable
Add Footer functionality to ALVgrid report
Enhanced ALV grid display
Add Event functionality to ALVgrid report
Example Output from ALV grid display
Example Output from ALV grid display with report heading
Add User command functionality to ALVgrid report
Add Title heading to ALV Grid incluing image or logo
Row selection of ALV grid
Add User command functionality to ALVgrid report
ALV list display
ALV list display(ALV_LIST)
Example ALV report output
Internal table structure required for FM 'REUSE_ALV_FIELDCATALOG_MERGE'
Multiple ALV list display
Various ways to build an ALV Fieldcatalog within SAP
Add image to ALV report using OAOR tcode, without replacing the enjoy SAP logo
Transport an ALV variant with SAP