Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  SORT itab 

Sorting Internal Tables Alphabetically

This example demonstrates the alphabetical sorting of character strings.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: BEGIN OF line,
            text(6) TYPE c,
            xtext TYPE xstring,
          END OF line.

    DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY text.

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

    line-text = 'Muller'(001).
    CONVERT TEXT line-text INTO SORTABLE CODE line-xtext.
    INSERT line INTO TABLE itab.

    line-text = 'M�ller'(002).
    CONVERT TEXT line-text INTO SORTABLE CODE line-xtext.
    INSERT line INTO TABLE itab.

    line-text = 'Moller'(003).
    CONVERT TEXT line-text INTO SORTABLE CODE line-xtext.
    INSERT line INTO TABLE itab.

    line-text = 'Miller'(004).
    CONVERT TEXT line-text INTO SORTABLE CODE line-xtext.
    INSERT line INTO TABLE itab.

    SORT itab.
    out->write_data( itab ).

    SORT itab BY xtext.
    out->write_data( itab ).

    SORT itab AS TEXT.
    out->write_data( itab ).

    out->display( ).

Description

The table itab contains a column of text fields and a column of associated country-specific sortable binary characters that were created in a sortable format using conversions. The table is sorted three times. First, a binary search is performed on the key field text, then a binary search on the xtext field, and finally an alphabetical search on the key field text.

In the first sort 'Möller' comes after 'Muller', because the internal representation of 'ö' comes after the representation for 'u'. Both the other sorts are alphabetical. The binary sort performed on xtext has the same result as an alphabetical sort on the field text.