e recently had a requests in a report to offer sorting options at selection time (not in the ALV Grid later).

So basically what you do is offer Parameters for Sortorder or even better a restricted Select-Option if you have a Domain of the Columns.

Then you have a iTab representing the Sort Order in the ata Definition.

TYPESBEGIN  OF SORT_HELP,
          POS     TYPE N LENGTH 2,
          TABNAME TYPE YI_BDSCOLS.
TYPESEND    OF SORT_HELP.


DATA: IT_SORTLIST          TYPE TABLE OF SORT_HELP,
     VAR_SORTITEM         TYPE SORT_HELP.


You can write a Function to add Columns to the sorting

*&---------------------------------------------------------------------*
*&      Form  ADD_SORTITEM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->F_POS  Position of Col in Sorting
*      -->F_TABNAME Col Name
*----------------------------------------------------------------------*
FORM CUST_ADD_SORTITEM  USING    F_POS TYPE N
                            F_TABNAME TYPE CHAR20.

  IF F_POS > .
    CLEAR VAR_SORTITEM.
    READ TABLE IT_SORTLIST WITH KEY POS F_POS TRANSPORTING NO FIELDS.
    "Wenn Schlüssel schon vorhanden haben wir eine doppelte Zeile
    IF SY-SUBRC 0.
        MESSAGE E685(yb).
    ELSE.
      VAR_SORTITEM-TABNAME F_TABNAME.
      VAR_SORTITEM-POS F_POS.
      APPEND VAR_SORTITEM TO IT_SORTLIST.
      SORT IT_SORTLIST BY POS ASCENDING.
    ENDIF.

  ENDIF.

ENDFORM.                    " ADD_SORTITEM


And wherever you fill your ALV Grid you generate the Sorting upfront:

*# Sort
  DATA L_WA_SORT                  TYPE LVC_S_SORT.          "#EC *

  CLEAR C_T_SORTREFRESH C_T_SORT.

  LOOP AT IT_SORTLIST INTO VAR_SORTITEM.

    CLEARL_WA_SORT.
    L_WA_SORT-SPOS        SY-TABIX.           "Level of Sortierung
    L_WA_SORT-FIELDNAME   VAR_SORTITEM-TABNAME.  "Sortcolumn
    L_WA_SORT-UP          'X'.         "Sortierung ascending
    L_WA_SORT-DOWN        ' '.         "Sortierung descending
    APPEND L_WA_SORT TO C_T_SORT.

  ENDLOOP.

The neat thing now is, that you also are able to redo the Fieldcat of the ALV Grid in Order of the Sorting upfront.

I did it like this, there may be a better way. Comments are welcome.



  DATAL_IT_FIELDCAT_NEW TYPE LVC_T_FCAT,
        L_VAR_COL_POS_CNT TYPE VALUE 1.

  FIELD-SYMBOLS <L_WA_FIELDCAT_COL> TYPE LVC_S_FCAT.

  "Copy all FieldCat Fields not associated with viewable Cols
  LOOP AT C_T_FIELDCAT ASSIGNING <L_WA_FIELDCAT_COL>
      WHERE TABNAME EQ ''.
    APPEND <L_WA_FIELDCAT_COL> TO L_IT_FIELDCAT_NEW.
    DELETE C_T_FIELDCAT.
  ENDLOOP.

  "Insert Cols from the SortList and remove them from the initial Fieldcat
  LOOP AT IT_SORTLIST INTO VAR_SORTITEM.
    READ TABLE C_T_FIELDCAT
                WITH KEY FIELDNAME VAR_SORTITEM-TABNAME
                INTO L_WA_FIELDCAT.
    IF SY-SUBRC 0.
      DELETE C_T_FIELDCAT
        WHERE FIELDNAME VAR_SORTITEM-TABNAME.
    ENDIF.
    APPEND L_WA_FIELDCAT TO L_IT_FIELDCAT_NEW.
  ENDLOOP.

  "Now add all Cols from the Initial Fieldcat after Sorted Ones
  LOOP AT C_T_FIELDCAT ASSIGNING <L_WA_FIELDCAT_COL> .
    APPEND <L_WA_FIELDCAT_COL> TO L_IT_FIELDCAT_NEW.
  ENDLOOP.

  "At last you have to assign new Col_Positions for all Entriew in the FieldCat
  LOOP AT L_IT_FIELDCAT_NEW ASSIGNING <L_WA_FIELDCAT_COL>
    WHERE TABNAME NE ''.
    <L_WA_FIELDCAT_COL>-COL_POS L_VAR_COL_POS_CNT.
    L_VAR_COL_POS_CNT L_VAR_COL_POS_CNT + 1.
  ENDLOOP.

  C_T_FIELDCAT L_IT_FIELDCAT_NEW.






Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com