Virtual Sorting of Internal Tables
With ABAP 7.52 a new method CL_ABAP_ITAB_UTILITIES=>VIRTUAL_SORT available with which we can get the sorted row index table of the main source internal table without changing the source internal table.
Later using this sorted row index table, various sorted form of the actual source data can be formed.
By default SORT is ascending.
DATA: lt_spfli_asc TYPE TABLE OF spfli.
DATA: lt_virtual_source TYPE cl_abap_itab_utilities=>virtual_sort_table.
SELECT * FROM spfli INTO TABLE @DATA(lt_spfli).
lt_virtual_source = VALUE #(
(
source = REF #( lt_spfli )
components = VALUE #( ( name = 'COUNTRYFR') )
)
).
cl_abap_itab_utilities=>virtual_sort(
EXPORTING
im_virtual_source = lt_virtual_source
RECEIVING
rt_virtual_index = DATA(lt_vir_sort_asc_index) ).
cl_demo_output=>display_data(
EXPORTING
value = lt_spfli ).
cl_demo_output=>display_data(
EXPORTING
value = lt_vir_sort_asc_index ).
LOOP AT lt_vir_sort_asc_index ASSIGNING FIELD-SYMBOL().
APPEND lt_spfli[ ] TO lt_spfli_asc.
ENDLOOP.
cl_demo_output=>display_data(
EXPORTING
value = lt_spfli_asc ).
The actual source table is as below.
With sort, we can receive the row index table as follow.
When above row index applied to the source table, the final virtual sorted table as below.
For descending SORT, the pass as below
DATA: lt_spfli_des TYPE TABLE OF spfli.
SELECT * FROM spfli INTO TABLE @DATA(lt_spfli).
cl_abap_itab_utilities=>virtual_sort(
EXPORTING
im_virtual_source = VALUE #(
(
source = REF #( lt_spfli )
components = VALUE #( ( name = 'COUNTRYFR' descending = 'X' ) )
)
)
RECEIVING
rt_virtual_index = DATA(lt_vir_sort_des_index) ).
cl_demo_output=>display_data(
EXPORTING
value = lt_spfli ).
cl_demo_output=>display_data(
EXPORTING
value = lt_vir_sort_des_index ).
LOOP AT lt_vir_sort_des_index ASSIGNING FIELD-SYMBOL().
APPEND lt_spfli[ ] TO lt_spfli_des.
ENDLOOP.
cl_demo_output=>display_data(
EXPORTING
value = lt_spfli_des ).