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 Flight Data

This example demonstrates the virtual sorting of an internal table containing flight data.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    cl_demo_output=>new(

      )->next_section(
      `Ascending Sort by Latitude, Longitude of CITYFROM, CITYTO`

      )->write( VALUE flights(
                  FOR <idx>
                  IN cl_abap_itab_utilities=>virtual_sort(
                       im_virtual_source =
                         VALUE #(
                           ( source     = REF #( from_city_tab )
                             components =
                               VALUE #( ( name = 'latitude' )
                                       ( name = 'longitude' ) ) )
                           ( source     = REF #( to_city_tab )
                             components =
                               VALUE #( ( name = 'latitude' )
                                       ( name = 'longitude' ) ) )
                           ( source     = REF #( flight_tab )
                             components =
                               VALUE #( ( name = 'carrid' )
                                       ( name = 'connid' ) ) ) ) )
                  ( flight_tab[ <idx> ] ) )

      )->next_section(
      `Descending Sort by Latitude, Longitude of CITYFROM, CITYTO`

      )->write( VALUE flights(
                  FOR <idx>
                  IN cl_abap_itab_utilities=>virtual_sort(
                       im_virtual_source =
                         VALUE #(
                           ( source     = REF #( from_city_tab )
                             components =
                               VALUE #( ( name = 'latitude'
                                         descending = abap_true )
                                       ( name = 'longitude'
                                         descending = abap_true ) ) )
                           ( source     = REF #( to_city_tab )
                             components =
                               VALUE #( ( name = 'latitude'
                                         descending = abap_true )
                                       ( name = 'longitude'
                                         descending = abap_true ) ) )
                           ( source     = REF #( flight_tab )
                             components =
                               VALUE #( ( name = 'carrid' )
                                       ( name = 'connid' ) ) ) ) )
                  ( flight_tab[ <idx> ] ) )

      )->display( ).

Description

In this example, an internal table flight_tab containing flight data is sorted virtually by the longitudes and latitudes of the departure and arrival cities. To do this, two additional tables from_city_tab and to_city_tab are constructed. The rows of these tables contain the longitudes and latitudes of the departure and arrival cities from the respective rows of flight_tab.

Virtual sorting with the method VIRTUAL_SORT of class CL_ABAP_ITAB_UTILITIES takes place at the operand position of a FOR expression for a table iteration. The virtual sorting involves all three internal tables. The temporary result of the sorting is used to construct a sorted internal table from the rows of flight_tab. This table is only temporary, and is an input parameter of the method WRITE of class CL_DEMO_OUTPUT.

It is sorted once in ascending order and once in descending order. This does not change the order of the rows in the internal tables that are involved. These remain in their original unsorted state. Virtual sorting makes it possible to generate various sorted output data without affecting the original data.