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 itab

Former Member
0 Kudos

Hi Experts,

i have an internal table with data as follows:

matnr date balance

b 20061010 0

a 20061016 10

a 20061016 20

a 20061016 30

before sorting the internal has data like the above.

But after the statement:

sort itab by matnr date.

the internal table itab is like this:

matnr date balance

a 20061016 30

a 20061016 20

a 20061016 10

b 20061010 0

how the order of the balance changed from 10,20,30 to 30,20,10.

if i want to get the data as follows:

matnr date balance

a 20061016 10

a 20061016 20

a 20061016 30

b 20061010 0

what should i do?

Please help.

Regards,

Soumya

13 REPLIES 13

Former Member
0 Kudos

Hi!

sort itab by matnr date balance.

Regards

Tamá

Former Member
0 Kudos

sort itab by matnr date balance.

Former Member
0 Kudos

Hi Soumya,

Try using

SORT ITAB BY MATNR DATE <b>ASCENDING</b>.

<b>Reward points for helpful answers</b>.

Best Regards,

Ram.

Former Member
0 Kudos

Hi,

Try this.

sort itab by matnr date <b>balance</b>.

Former Member
0 Kudos

hi

sort itab matnr date balance.

Satya.

Former Member
0 Kudos

hi

may b the records in the table were inserted in that order....

first a 20061016 30 then the next....

Former Member
0 Kudos

hi,

when u sort ur table try this ;:

sort itab by matnr balance.

if this does not give u the results try

sort matnr date balance.

reward with points if useful

thank

nayan

Former Member
0 Kudos

hi,

sort itab by matnr date balance.

Reward with points if helpful.

Former Member
0 Kudos

Hi Soumya,

Please try :

sort itab ascending by matnr date balance.

Reward points if useful.

Rgds,

Preeti

Former Member
0 Kudos

i don't think you will get the desired results by a standard sort statement...

you want to sort on matnr by ascending and date descending - this is possible

sort itab by matnr ascending date descending.

you want to put the zero values at the end for balance....while keeping the rest of the values ascending.....this is not possible by the sort statement

do this manually

first sort it on balance by ascending

sort itab by matnr ascending date descending balance ascending.

then loop on the table and wherever you find zero for balance...put it at the end

loop at itab into wa.

i_tabix = sy-tabix.

if wa-balance eq 0.

append wa to itab.

delete itab index i_tabix.

endif.

endloop.

hope this helps...

forgot to mention.....do remember to adjust this code a bit...else it will lead to an infinite loop......

for this...may be you can store the number of lines originally in the table and proceed with the append-delete thing only up to that value of sy-tabix.

Message was edited by:

Priyank Jain

Message was edited by:

Priyank Jain

0 Kudos

Hi,

My problem is not regarding the balance.

before sorting the itab was like this:

matnr date balance

a 20061016 10

a 20061016 20

a 20061016 30

b 20061010 0

i am sorting based on the matnr and date field.

since the matnr and date field values for the first three entries are the same, i should keep it it like that only.

the problem is after sorting , it reversed like this:

matnr date balance

a 20061016 30

a 20061016 20

a 20061016 10

why it happened so?

0 Kudos

hi,

Ya but what is the problem in sorting itab by balance also as every one suggest?

0 Kudos

Hi all,

My problem is solved using STABLE addition like below:

sort itab stable by matnr date.

Regards,

Soumya