Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  System Class for Internal Tables →  Examples of Virtual Sorting of Internal Tables 

Virtual Sorting of an Internal Table

This example demonstrates the virtual sorting of a single internal table.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TYPES:
      BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
        col3 TYPE string,
        col4 TYPE string,
      END OF line,
      itab TYPE STANDARD TABLE OF line WITH EMPTY KEY.

    DATA(rnd) = cl_abap_random_int=>create( seed = + sy-uzeit
                                           min  = 1
                                           max  = 10 ).

    DATA(itab) = VALUE itab( FOR i = 1 UNTIL i > 10
                             ( col1 = rnd->get_next( )
                               col2 = rnd->get_next( )
                               col3 = substring(
                                       val = sy-abcde
                                       off = rnd->get_next( ) - 1
                                       len = 1 )
                               col4 = substring(
                                       val = sy-abcde
                                       off = rnd->get_next( ) - 1
                                       len = 1 ) ) ).

    DATA(out) = cl_demo_output=>new( ).

    out->write( itab ).

    out->next_section( `Virtual Sort by col1, col2, Ascending` ).

    DATA(v_index) =
      cl_abap_itab_utilities=>virtual_sort(
        im_virtual_source = VALUE #(
         ( source     = REF #( itab )
           components = VALUE #( ( name = 'col1' )
                                ( name = 'col2' ) ) ) ) ).

    out->write( v_index ).

    DATA sorted_tab TYPE itab.
    sorted_tab = VALUE #( FOR idx IN v_index ( itab[ idx ] ) ).

    DATA(test_tab) = itab.
    SORT test_tab STABLE BY col1 col2.
    ASSERT sorted_tab = test_tab.

    out->write( sorted_tab ).

    out->next_section( `Virtual Sort by col3, col4, Descending` ).

    v_index =
      cl_abap_itab_utilities=>virtual_sort(
        im_virtual_source = VALUE #(
         ( source     = REF #( itab )
           components = VALUE #(
                         ( name = 'col3'
                           astext = abap_true
                           descending = abap_true )
                         ( name = 'col4'
                           astext = abap_true
                           descending = abap_true ) ) ) ) ).

    out->write( v_index ).

    sorted_tab = VALUE #( FOR idx IN v_index ( itab[ idx ] ) ).

    test_tab = itab.
    SORT test_tab STABLE BY col3 AS TEXT DESCENDING
                            col4 AS TEXT DESCENDING.
    ASSERT sorted_tab = test_tab.

    out->write( sorted_tab ).

    out->display( ).

Description

An internal table filled with random numbers and letters is sorted virtually using the method VIRTUAL_SORT of system class CL_ABAP_ITAB_UTILITIES, first in ascending order by the first two columns and then in descending alphabetical order by the last two columns. The returned array contains the row numbers in the respective sort order. This array is used to fill an internal table sorted_tab according to the sorting. To compare, an internal table test_tab with the same content is constructed and sorted with the statement SORT. The results are the same in both cases.