Dear SAPLearners, in this blog post we will learn about Virtual sorting in ABAP internal tables.
This blog post is part of What’s New in ABAP 7.52, check out other new features added in S/4 HANA 1809 and ABAP 7.52 release.
Also Read: Whats New Features added in ABAP 7.4 release.
Virtual Sorting
A new method VIRTUAL_SORT is available in the class CL_ABAP_ITAB_UTILITIES which enables virtual sorting of one or more internal tables.
This new concept is quite intriguing! 🤩 right?
Let’s take a step back and see how we were sorting internal tables? It’s well known to every ABAPer; it is by using a SORT statement. Here is the sample code for the same.
DATA(out) = cl_demo_output=>new( ).
SELECT bp_role,
email_address,
web_address,
bp_id,
company_name FROM snwd_bpa
INTO TABLE @DATA(lt_snwd_bpa).
SORT lt_snwd_bpa by company_name ASCENDING.
out->next_section(`Sort by COMPANY_NAME`).
out->write( lt_snwd_bpa ).
out->display( ).
The output is, the internal table is sorted by company_name in ASCENDING order.
So, then what Virtual Sort does?
Virtual Sorting will also sort the internal tables in the same way SORT does along with all other options like ASCENDING, DESCENDING etc.
Having said that, the only difference is, the output is an array of row numbers of the virtually sorted internal table.
Below is the ABAP code snippet using virtual sorting in ABAP internal table.
DATA(out) = cl_demo_output=>new( ).
SELECT bp_role,
email_address,
web_address,
bp_id,
company_name FROM snwd_bpa
INTO TABLE @DATA(lt_snwd_bpa).
DATA(lt_index) =
cl_abap_itab_utilities=>virtual_sort(
im_virtual_source = VALUE #( ( source = REF #( lt_snwd_bpa )
components = VALUE #( ( name = 'company_name' ) )
) ) ).
out->next_section(`Virtual Sort by COMPANY_NAME`).
out->write( lt_index ).
out->display( ).
As said earlier, the result lt_index is an array of row numbers of the virtually sorted internal table records.
Now you can use this new internal table lt_index which contains sorted row numbers, for further operations in the program.
For example, you can write the below code to fill the lt_sorted_tab according to sorting order.
DATA: ty_sorted_tab LIKE LINE OF lt_snwd_bpa,
lt_sorted_tab LIKE STANDARD TABLE OF ty_sorted_tab.
lt_sorted_tab = VALUE #( FOR idx IN lt_index ( lt_snwd_bpa[ idx ] ) ).
🧐 Observation: The original internal table lt_snwd_bpa is still intact which is not the case while using the SORT statement.
Okay, this is cool!! Now take a step forward to understand other features.
Virtual Sorting of an Internal Table with Filters
If we need only particular row numbers to be sorted, not the entire internal table, in this scenario lets see how we can use below virtual sorting.
Below is a sample ABAP code snippet, where I need to sort only the rows of BP Role is ‘02’.
DATA(out) = cl_demo_output=>new( ).
SELECT bp_role,
email_address,
web_address,
bp_id,
company_name FROM snwd_bpa
INTO TABLE @DATA(lt_snwd_bpa).
DATA lt_role02 type standard table of i with empty key.
lt_role02 = VALUE #( FOR <fs> IN lt_snwd_bpa INDEX INTO idx WHERE ( bp_role = '02' ) ( idx ) ).
DATA(lt_index) =
cl_abap_itab_utilities=>virtual_sort(
im_virtual_source = VALUE #( ( source = REF #( lt_snwd_bpa )
components = VALUE #( ( name = 'company_name' ) )
) )
im_filter_index = lt_role02
).
out->next_section(`Virtual Sort BP-ROLE 02 by COMPANY_NAME`).
out->write( lt_index ).
out->display( ).
In the above code i retrieved all the row numbers of BP Role = ‘02’ using FOR table iterator and later passed on the row numbers to VIRTUAL_SORT method.
The result lt_index in this case will return an array containing exactly these row numbers in the sort order.
Conclusion
Virtual sorting makes it possible to generate various sorted output data without affecting the original internal table data.
Now you know about Virtual Sorting in ABAP internal tables, next time when you want to sort an internal table try using this feature and let me know your feedback.
Please feel free to comment and let us know your feedback. Subscribe for more updates.
If you liked it ❤️, please share it! Thanks! 🙏