Use the following example in your program:
* Dynamically sort data using table key
DATA: ls_sort_key_field TYPE abap_sortorder.
DATA: lt_sort_key_field TYPE abap_sortorder_tab.
LOOP AT it_key INTO ls_key.
ls_sort_key_field-name = ls_key-fieldname.
APPEND ls_sort_key_field TO lt_sort_key_field.
ENDLOOP.
TRY.
SORT BY (lt_sort_key_field).
CATCH cx_sy_dyn_table_ill_comp_val.
* Unable to process dynamic sort
ENDTRY.