How to sort columns by default in SAP ALV List.

The colums can be sorted by default in the ALV table. So that it is sorted initially when it is first displayed.

              The internal table of type LVC_T_SORT can be used for this purpose.

              The following key fields of LVC_T_SORT to be filled are:

Fieldname: The column to be sorted must be mentioned in this field.

UP: If the column must be sorted in increasing or ascending order,then the value ‘X’ must be set to the field ‘UP’.

DOWN: If the column must be sorted in descending or decreasing order, then the value ‘X’must be set to the field ‘DOWN’.

SPOS: If two or more columns must be sorted. Then we can prioritize the sorting sequence. For example, second column must be sorted before the first column.

Let us look at the ALV table sorting with the following example.

DATA lt_sort TYPE lvc_t_sort.

DATA ls_sort TYPE lvc_s_sort.

              ls_sort-spos = 1.

              ls_sort-fieldname = ‘CONTACT_NAME’.

              ls_sort-up = ‘X’.

              APPEND ls_sort TO lt_sort.

              ls_sort-spos = 2.

              ls_sort-fieldname = ‘CONTACT_ID’.

              ls_sort-down =’X’.

              APPEND ls_sort TO lt_sort.

              CALL METHOD lr_gui_alv_grid -> set_table_for_first_display

                            EXPORTING

                                          is_layout = ls_layout

                            CHANGING

                                          it_outtab = lt_contact

                                          it_fieldcatalog = lt_field_cat

                                          it_sort = lt_sort.