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: 

loop to sort internal table

Former Member
0 Kudos
346

hi,

i would like to know if this is possible.

i would like to sort internal table base on fieldX. if fieldX is initial then sort with condition as below else system default sort sequence.

i know the syntax incorrect. need advice with example.

loop at itab

if itab-fieldX is initial.

sort itab[] by fieldA ascending fieldB descending.

endif.

endloop.

thanks

6 REPLIES 6

Former Member
0 Kudos
186

U dont need loop at for sorting.

What exactly are u cheking FieldX

sort itab[] by fieldA ascending fieldB descending. would be sufficient

Awrd Points if useful

Bhupal

Former Member
0 Kudos
186

Hi,

Declare another internal table of the same type as ITAB.

Loop at ITAB check the field and sort the new internal table in the loop.

DATA ITAB_NEW like ITAB.

loop at itab

if itab-fieldX is initial.

sort itab_new[] by fieldA ascending fieldB descending.

endif.

endloop.

Regards,

Pankaj

Former Member
0 Kudos
186

this is not a way.

would you please specify about the requirement?

Regards

ANUPAM

jayakummar
Active Participant
0 Kudos
186

hai,

better you can use read command for your validations.

dont use sort command inside the loop bcose evry loop count itab has altered .

reward points if useful.

regards,

jai.m

Former Member
0 Kudos
186

Hi,

The reqt is quite different.

Because in a internal table we may have some 100 records.

In that 100 records for 10 records you have fieldX is initail.And for some other 10 records fieldY is initial.

And for some 10 records both fieldx fieldY are initial means you cant do with loop processing.

You can do in otherway ..

v_fieldx_count = 0. "Type I

v_fieldy_count = 0.

loop at itab.

if itab-fieldX is initial AND fieldY is initial .

v_fieldx_count = v_fieldx_count + 1.

v_fieldy_count = v_fieldy_count +1.

elseif itab-fieldX is initial.

v_fieldx_count = v_fieldx_count + 1.

elseif itab-fieldY is initial.

v_fieldY_count = v_fieldY_count + 1.

endif.

endlloop.

If v_fieldx_count GE v_fieldY_count .

sort itab by fieldX.

else.

sort itab by fieldY.

Endif.

Like that you can proceed.

Regds

Parvathi

Please reward points if helpful.............

former_member402443
Contributor
0 Kudos
186

Hi,

You need not required to loop at internal table for sorting the table.

just use the sort statement followed by fields on which you want to sort the internal table.

sort itab by field1 ascending field2 descending.

Reward Points, if useful.

Regards,

Manoj Kumar