Application Development and Automation 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: 
Read only

Sorting an internal table with type string - Dynamic programing

Peter_Inotai
Active Contributor
0 Likes
1,479

Hi,

I have an internal table type string, which should be sorted based on a column name.

Column name and the table type are only known during runtime.

My idea is to build a formated internal table, sort it and copy back. However I'm stuck, as I have no idea how to do an APPEND for example.

I'm not even sure if this would be the way it should be done.

Any idea?

Thanks in advance,

Peter

My program part so far:


METHOD sort_searchresult .

  DATA:
    dref_line                TYPE REF TO data,
    dref_table               TYPE REF TO data.

  FIELD-SYMBOLS:
    <lwa_raw>      TYPE ANY,
    <lwa_typed>    TYPE ANY,
    <lit_typed>    TYPE ANY.


*--------------------------------------------------------------------------------------------------

  IF column IS NOT INITIAL.

    CREATE DATA dref_line TYPE (shlp_info-intdescr-selmethod).
    CHECK sy-subrc = 0.

    ASSIGN dref_line->* TO <lwa_typed>.
    CHECK sy-subrc = 0.

*
    CREATE DATA dref_table TYPE  STANDARD TABLE OF (shlp_info-intdescr-selmethod).
    CHECK sy-subrc = 0.

    ASSIGN dref_table->* TO <lit_typed>.
    CHECK sy-subrc = 0.


    LOOP AT t_searchresult_internal ASSIGNING <lwa_raw>.

      <lwa_typed> = <lwa_raw>.

*      APPEND <lwa_typed> TO <lit_typed>. ????

    ENDLOOP.

*    SORT <lit_typed> BY (column).

*    refresh t_searchresult_internal.
*    t_searchresult_internal[] = <lit_typed>[].


  ENDIF.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,223

hi,

Try with INSERT

INSERT  <lwa_typed>  INTO TABLE <lit_typed>.

10 REPLIES 10
Read only

Former Member
0 Likes
1,224

hi,

Try with INSERT

INSERT  <lwa_typed>  INTO TABLE <lit_typed>.

Read only

0 Likes
1,223

Thanks, unfortunately I got the message ""<LIT_TYPED>" is not an internal table."

Also tried the following, however then I got the message "The Dictionary structure or table "<LIT_TYPED>" is either not active or does not exist."

     INSERT INTO <lit_typed> ASSIGNING <lwa_typed>.

Any idea?

Thanks,

Peter

Read only

0 Likes
1,223

Actually I realized, that I can write the following and it works fine:

    <lit_typed> =  t_searchresult_internal[].

However sorting fails

"SORT <lit_typed> BY (column)." is not accepted.

Any idea on this?

Thanks,

Peter

Read only

0 Likes
1,223
this seems to be working for me , whats the problem 

data : begin of itab occurs 0,
            matnr like mara-matnr,
         end of itab.

parameters : p_col(5) default 'MATNR'.

itab-matnr = '456'.
append itab.
clear itab.

itab-matnr = '123'.
append itab.
clear itab.

sort itab by (p_col).
Read only

0 Likes
1,223

SORT <lit_typed> BY (column). should work

make sure that the column name passed to variable column is in uppercase.

Raja

Read only

0 Likes
1,223

Hi Raja,

Thanks for your help.

I cannot even activate my method, I got the message, that ""<LIT_TYPED>" is not an internal table."

Any idea?

Thanks,

Peter

Read only

0 Likes
1,223

Problem is with the internal table and not the sort by part.

Peter

Read only

0 Likes
1,223

If I change the definition to the following, than it can be activated...let me check how it looks in the debugger:

    <lit_typed>    TYPE STANDARD TABLE.

Read only

0 Likes
1,223

sorry i didnt notice it.

change it to

<lit_typed> TYPE ANY TABLE.

Read only

Peter_Inotai
Active Contributor
0 Likes
1,223

Thanks a lot for everyone, who helped me!!!!

It works fine now with the following code:


METHOD sort_searchresult .

  DATA:
    dref_table               TYPE REF TO data.

  FIELD-SYMBOLS:
    <lit_typed>    TYPE STANDARD TABLE.

*--------------------------------------------------------------------------------------------------

  IF column IS NOT INITIAL.

    CREATE DATA dref_table TYPE  STANDARD TABLE OF (shlp_info-intdescr-selmethod).
    CHECK sy-subrc = 0.

    ASSIGN dref_table->* TO <lit_typed>.
    CHECK sy-subrc = 0.

*   Move to formatted itab
    <lit_typed> =  t_searchresult_internal[].

*   Sort
    SORT <lit_typed> BY (column).

*   Move back SORTED itab
    REFRESH t_searchresult_internal[].
    t_searchresult_internal[] =  <lit_typed>.


  ENDIF.

ENDMETHOD.