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: 

For all entries sorting problem

Former Member
0 Kudos


I have a problem  sorting data when for all entries is used

for example i got it_pernr like this

row  pernr

1      5

2      2

3       8


Then i got the selection

SELECT pernr test_column

       FROM test_table INTO CORRESPONDING FIELDS OF TABLE it_test_table

       FOR ALL ENTRIES IN it_pernr

           WHERE pernr = it_pernr-pernr

            AND begda <= sy-datum

            AND endda >= sy-datum.

After selection i got table it_test table like this :

row    pernr    test_column

1         2          test2

2         8           test8

3         5          test5

So my question is how can I sort it like this ( like in it_pernr):

row    pernr    test_column

1         5          test5

2         2          test2

3         8          test8

Thanks in advance

1 ACCEPTED SOLUTION

atul_mohanty
Active Contributor
0 Kudos

1. Create one more table with same structure as it_test say it_test1

2. Loop over the table  it_pernr

  Read it_test into w_test with key  it_pernr-pernr

   If sy-subrc = 0

append w_test  to it_test1.

endif.

endloop.

3. clear it_test[].

4. it_test[] = it_test1[].

7 REPLIES 7

Former Member
0 Kudos

Hi,

TRy below code.

sort IT_TEST_TABLE by pernr.

Loop at IT_PERNR into WA_PERNR.

read table IT_TEST_TABLE into WA_TAB with key pernr = wa_pernr-pernr binary search.

if sy-subrc = 0.

   apppend  WA_TAB to it_new_sort.

endif.

endloop.

IT_NEW_SORT will be in the order of IT_PERNR.

Regards

Sree

Former Member
0 Kudos

Hi,

You are getting given output in shown format because row, pernr is key field.

If you want the output in desired format then create 1 more internal table ITBA3 with three field-row, pernr, test_column.

sort it_test_table by pernr.

Loop on it_pernr into wa_pernr.

wa_itab3-row = wa_pernr-row.

wa_itab3-pernr = wa_pernr-pernr.

read table it_test_table into wa_test with key pernr =  wa_pernr-pernr.

if sy-subrc eq 0.

wa_itab3-test_column = wa_test-test_column.

endif.

if wa_itab3 is not initial.

append wa_itab3 to it_tab3.

endif.

clear:wa_itab3, wa_pernr, wa_test.

endloop.

Now your it_tab3 is desired output.

If you have any query, let me know.

Regards,

Sudeesh Soni

atul_mohanty
Active Contributor
0 Kudos

1. Create one more table with same structure as it_test say it_test1

2. Loop over the table  it_pernr

  Read it_test into w_test with key  it_pernr-pernr

   If sy-subrc = 0

append w_test  to it_test1.

endif.

endloop.

3. clear it_test[].

4. it_test[] = it_test1[].

Former Member
0 Kudos

If you want both to be in the same order, why not sort both the tables on pernr itself.

Also SORT means order either ascending or descending what order is 5,2,8 ????

Regards

Former Member
0 Kudos

There is no sorting problem. Your first internal table is actually not sorted.

When you use for all entries, system will give data in sorted order.

If your requirement is to get the order as per first internal table, then u can write extra logic to get desire output.

Former Member
0 Kudos

I got the solution thank u all for your help

Former Member
0 Kudos

hi

Please mark the correct answer or mark helpful answer and close the thread.

Regards,

Sudeesh Soni