03-12-2009 11:06 AM
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
03-12-2009 11:10 AM
Hi,
If you can change the structure of your internal table keep vbtyp as first field and sort by vbtypp Descending..
Thanks.
03-12-2009 11:10 AM
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
03-12-2009 11:11 AM
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.
03-12-2009 11:11 AM
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.
03-12-2009 11:12 AM
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.
03-12-2009 11:13 AM
hi:
Just before loop.
sort <table name> by vbeln vbtyp posnr
Regards
Shashi
03-12-2009 11:15 AM
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.
03-12-2009 11:17 AM
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
03-12-2009 11:18 AM
03-12-2009 11:24 AM
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.
02-09-2011 11:07 AM