Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do you sort a dynamic table pointed to by a field symbol?

Former Member
0 Kudos

I have an abap class with a method which accepts any table. I do this by specifying an IMPORTING parameter of TYPE REF TO DATA.

I am working with this type reference using field symbols. But not knowing which fields are present, how do I sort this table by the supplied sort fields?

For example, I passed a table to this method with three columns, BUKRS, VBELN, and AUDAT. Note that I can pass anything to this method so hardcoding is not acceptable. I also pass a table of STRINGs that contain the field names to be sorted (let's say it contains "AUDAT" and "VBELN" so that the table should be sorted by AUDAT, then VBELN)

If I try to sort the field symbol assigned to the dynamic table (say <fs_tab>) using the concatenated sort criteria, i get the runtime error ITAB_ILLEGAL_COMPONENT.


    ...
    " concatenate the contents of sort table into sort_by (separated by space)
    " sort_by is now equal to `AUDAT  VBELN`

    " if i do this
    SORT <fs_tab> BY (sort_by)

    " I get this:
    " > The row type "S_DATA" of the internal table "<FS_ITAB>" does not contain a component " AUDAT VBELN".

If I try to hardcode the fields to the field symbol, they are not recognized by the interpreter


    " If I do this
    SORT <fs_tab> BY audat vbeln.

    " I get this
    " > The specified type has no structure and therefore no component called "AUDAT"

Please help.

Thanks in advance!

--

Kyle

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Take two field-symbols:-

<f1> type audat
<f2> type vbeln

now assign component audat of <fs_tab> into <f1>
and assign component vbeln of <fs_tab> into <f2>.

now do 
sort <fs_tab> by <f1> <f2>.

please revert back for more help.

Rgds/K rathi

11 REPLIES 11

Former Member
0 Kudos

Hi,

Take two field-symbols:-

<f1> type audat
<f2> type vbeln

now assign component audat of <fs_tab> into <f1>
and assign component vbeln of <fs_tab> into <f2>.

now do 
sort <fs_tab> by <f1> <f2>.

please revert back for more help.

Rgds/K rathi

0 Kudos

Thanks K rathi,

But since I the method can accept any table with any number of columns, and can be sorted by any number of columns in that table, I cannot really hardcode the field symbols that point to the sort fields.

Kyle

Former Member
0 Kudos

Hi Kyle

Please mention your exact requirement.

Thanks

K Rathi

Former Member
0 Kudos

I am making a class which is like a simplified version of ALV list but works and displays things in a different way (this is why I decided not to use ALV).

Prior to displaying the data, it needs to be sorted if the calling program specified the fields to be sorted. To illustrate this, please refer to the following method:


method DO_SORT.

    " GLOBAL ATTRIBUTES:
    " listdata TYPE REF TO DATA  <- any type of table wil be referenced by this pointer
    " sortcat TYPE TABLE OF [some structure that has a NAME component, which is of TYPE STRING]

    DATA:
        sortby TYPE STRING,
        wa_sort LIKE LINE OF sortcat
    .

    FIELD-SYMBOLS:
        <fs_itab> TYPE ANY TABLE
    .

    ASSIGN listdata->* TO <fs_itab>.

    LOOP AT sortcat INTO wa_sort.
        CONCATENATE '(' wa_sort-name ')' INTO wa_sort-name.
        CONCATENATE wa_sort-name 'ASCENDING' INTO wa_sort-name SEPARATED BY SPACE.
        CONCATENATE sortby wa_sort-name INTO sortby SEPARATED BY SPACE.
    ENDLOOP.

    CONDENSE sortby.

    SORT <fs_itab> BY (sortby).

endmethod.

However, this does not work.

I am having problems with accessing the components of the field symbol to <fs_itab> since it is assigned to a generic data type DATA.

Thanks in advance!

--

Kyle

Edited by: Kyle Domingo on Jan 29, 2010 7:55 AM

0 Kudos

Hello,

Does this help:

DATA:
SORT_F1 TYPE FIELDNAME,
SORT_F2 TYPE FIELDNAME.

sort_f1 = 'AUDAT'.
sort_f2 = 'VBELN'.

SORT <fs_tab> BY (sort_f1) (sort_f2).

BR,

Suhas

Former Member
0 Kudos

Hi,

As you are getting field-names in sortby, these fields must be seperated by space.

So split the sortby into internal table using SPACE as the delimiter.

now you have got the actual components.

assign these componenets to some other <fs>( type any).

and then try to sort.

Rgds/K rathi

matt
Active Contributor
0 Kudos

Krathi is pointing you to Addition 5 of the SORT statement, in the ABAP Help. SORT .... BY (otab). The documentation is quite clear on how to use this. It is a release 7.0 feature. If you're using an earlier version, you'll need to use GENERATE SUBROUTINE.

matt

>

> SORT .... BY (otab). The documentation is quite clear on how to use this. It is a release 7.0 feature. If you're using an earlier version, you'll need to use GENERATE SUBROUTINE.

Hello Matt,

I am in ECC5.0 & i think i have this feature available. I wrote this sample code & works great for me:

DATA: v_data TYPE REF TO data.

FIELD-SYMBOLS:
        <fs_tab> TYPE ANY TABLE.

DATA:
sort_f1 TYPE fieldname,
sort_f2 TYPE fieldname.

CREATE DATA v_data TYPE STANDARD TABLE OF t001.
ASSIGN v_data->* TO <fs_tab>.

SELECT * FROM t001 INTO TABLE <fs_tab>.
IF sy-subrc = 0.
  sort_f1 = 'LAND1'.
  sort_f2 = 'WAERS'.

  SORT <fs_tab> BY (sort_f1) (sort_f2).

  WRITE 'Hurray !!! My table is sorted:-)'.
ENDIF.

Cheers,

Suhas

0 Kudos

had a similar problem and your explanation helped me perfectly!
Thank you!

Former Member
0 Kudos

Thanks Matt and Suhas,

Unfortunately, I do know that and as you can see from my sample code above, I was generating the string that contains the sort fields (enclosed in parentheses). Here it is:


    LOOP AT sortcat INTO wa_sort.
        CONCATENATE '(' wa_sort-name ')' INTO wa_sort-name.
        CONCATENATE wa_sort-name 'ASCENDING' INTO wa_sort-name SEPARATED BY SPACE.
        CONCATENATE sortby wa_sort-name INTO sortby SEPARATED BY SPACE.
    ENDLOOP.

The first CONCATENATE encloses the field name with parentheses. The second CONCAT was a test if it would work with the ASCENDING addition (notice the inclusion of SPACE). The third CONCAT puts all the sort fields in one string - since I do not know how many sort fields the user will specify.

The example of Suhas probably works because the program knows the type of v_data (STANDARD TABLE OF t001); whereas my class does not know the type of listdata (TYPE REF TO DATA). The variable listdata is not created inside the class, but instead being passed as a parameter.

Sorting using field symbols is also not supported in ABAP Objects making this problem really painful. I guess i'll just have to close this thread for now.

I think i'll just sort the table in the calling program. i'll create a separate thread on sorting a variable that is a TYPE TABLE OF REF TO zclass in the ABAP Objects section.

Thanks a lot experts!

--

Kyle

wilbur_peng
Explorer
0 Kudos

you may use sorted table or class cl_abap_itab_utilities for dynamic sort.

check my blog CSDN abap table dynamic sort for reference( Chinese version but code in ABAP)