Virtual Sorting of Internal Tables

light111Virtual 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.

1


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.

2

With sort, we can receive the row index table as follow.

3

When above row index applied to the source table, the final virtual sorted table as below.

4


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 ).

 


With descending sort, we can receive the row index table as follow.

5

When above row index applied to the source table, the final virtual sorted table as below.

6


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s