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: 

Sort internal table in a perticular order

satya_devaraju
Explorer
0 Kudos

HI,

I would like to sort internal table in a perticular order like first vbtyp = T , then vbtyp = O and then other types. It is neither ascending or descending . Is there any way to do this?

vbeln posnr vbtyp

1 001 A

2. 001 T

2 002 T

3 001 J

3 002 J

3 001 O

3 002 O

regards,

satya

11 REPLIES 11

jyothi_anagani
Active Contributor
0 Kudos

Hi,

If you can change the structure of your internal table keep vbtyp as first field and sort by vbtypp Descending..

Thanks.

I355602
Advisor
Advisor
0 Kudos

Hi,

Take a new field in internal table say temp TYPE i.

Now use:


loop at itab.
  if itab-vbtyp = 'T'.
    itab-temp = 1.
  elseif itab-vbtyp = 'O'.
    itab-temp = 2.
  else.
    itab-temp = 3.
  endif.
  modify itab index sy-tabix.
endloop.

sort itab by temp.

Hope this helps you.

Regards,

Tarun

vinod_vemuru2
Active Contributor
0 Kudos

HI Satya,

There r only 2 options available in SORT. Ascending n descending. Explain ur original requirement.

Why u want to sort in that particular order?

Other way is u can take the required data into another internal table.

Thanks,

Vinod.

Former Member
0 Kudos

for this you need to do long coding as follows.

loop at it where vbtyp = 'T'.

append wa to IT1.

endloop.

loop at it where vbtyp = 'O'.

append wa to IT1.

endloop.

loop at it where vbtyp = 'J'.

append wa to IT1.

endloop.

so IT1 will have the order as per your requirement.

Former Member
0 Kudos

Hi.. Declare on more field (INDEX type i)i n internal table. when appending entries into ITAB based on the condition like vbtyp = T then put 1 in INDEX like according to ur requirement. after append data into ITAB, sort based on INDEX field.

Regards,

KP.

Former Member
0 Kudos

hi:

Just before loop.

sort <table name> by vbeln vbtyp posnr

Regards

Shashi

Former Member
0 Kudos

The only way I can think of is to add another field to the table. call it sort.

loop at itab.

case itab-vbtyp.

when 'T'.

move '1' to itab-sort.

when 'O'.

move '2' to itab-sort.

when 'A'.

move '3' to itab-sort.

when 'J'.

move '4' to itab-sort.

when others.

move '5' to itab_sort.

endcase.

modify itab-sort.

endloop.

sort itab by sort.

if you need to you could use a different table to carry out the sort and use move-corresponding to move the data back to your table.

former_member228751
Contributor
0 Kudos

Hi Satya,

Declare one more interal table itab1 of same type of itab.

And use below code:

(itab is int table with fields vbeln posnr vbtyp)


Loop at itab into wa where vbtyp = 'T'.
 Append wa to itab1.
Endloop.

Loop at itab into wa where vbtyp = 'O'.
 Append wa to itab1.
Endloop.

Loop at itab into wa where vbtyp NE 'O' and vbtyp = 'T'.
 Append wa to itab1.
Endloop.

So finally in itab1 you will get desired entries.

Regards,

Sachin

Former Member
0 Kudos

sort table name by vbtyp posnr.

Regards,

Joan

0 Kudos

Hi,

Another way of doin it is using NESTED Loop. If ur data is not large this wont cause a performance issue.

Store ur seq in a Internal table "IT_TAB" with f1 and f2.

vbtyp 'T'

vbtyp 'O'

etc....

Nested Loop:

LOOP at IT_TAB into wa_tab.

loop at ur table and read for "fieldname-value" = wa_tab-f2.

Append to another table.

Endloop.

endloop.

satya_devaraju
Explorer
0 Kudos

thanks