Application Development and Automation 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: 
Read only

Issue with Sort statement

Former Member
0 Likes
1,296

Dear all,

Need your help for the following issue.

I intend to sort the internal table (given below) with endda in the descending order along with 2 other key fields, so i was expecting the record with endda '99991231' as the first entry in the internal table, but this is not happening.

REPORT ytest_sort.

DATA:li_p9 TYPE TABLE OF pa0009 WITH HEADER LINE.

li_p9-begda = '20090515'.

li_p9-endda = '20090520'.

li_p9-subty = '0'.

APPEND li_p9.

li_p9-begda = '20090521'.

li_p9-endda = '99991231'.

li_p9-subty = '0'.

APPEND li_p9.

SORT li_p9 BY endda subty seqnr DESCENDING.

DELETE ADJACENT DUPLICATES FROM li_p9 COMPARING subty seqnr.

WRITE 'sorted'.

Need your help in identifying the issue with the sort statement in the above code fragment.

Thanks & Regards,

Kiran N N

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,196

sneha,

Thankyou for your reply. If you observe the above code, both seqnr and subty have same values for both entries, so it should be solely dependant on the endda right?

11 REPLIES 11
Read only

Former Member
0 Likes
1,196

Hi,

Since your sort contains 3 fields it will work on the combination of these 3 and not on the field 'endda' alone..

please check this ... and sort accordingly..


SORT li_p9 BY endda subty seqnr DESCENDING. <--- Check the field 'seqnr'

Read only

Former Member
0 Likes
1,196

Hi Kiran

Can you please tell what is the field seqnr?

Where it is populated in the table?

Regards,

Deepa Kulkarni

Read only

Former Member
0 Likes
1,196

Hi,

you will have to give the DESCENDING additon after the field for which you want to sort in a descending fashion. Else by default it will take Ascending.

Hence your output is coming incorrectly.

So SORT as follows:-

SORT li_p9 BY endda DESCENDING subty DESCENDING seqnr DESCENDING.

Regards,

Ankur Parab

Read only

Former Member
0 Likes
1,197

sneha,

Thankyou for your reply. If you observe the above code, both seqnr and subty have same values for both entries, so it should be solely dependant on the endda right?

Read only

0 Likes
1,196

Hi,

In that case sort by adding descending for all the fields:


sort li_p9 endda descending subty descending seqnr descending .

or 

sort li_p9 endda descending subty seqnr.

Read only

Former Member
0 Likes
1,196

Hi,

REPORT ytest_sort.
DATA:li_p9 TYPE TABLE OF pa0009 WITH HEADER LINE.

li_p9-begda = '20090515'.
li_p9-endda = '20090520'.
li_p9-subty = '0'.
APPEND li_p9.
li_p9-begda = '20090521'.
li_p9-endda = '99991231'.
li_p9-subty = '0'.
APPEND li_p9.
SORT li_p9 BY endda DESCENDING subty seqnr .  " --> Change
DELETE ADJACENT DUPLICATES FROM li_p9 COMPARING subty seqnr.
WRITE 'sorted'.

Read only

Former Member
0 Likes
1,196

The value for seqnr for both the entries is "000".

Read only

Former Member
0 Likes
1,196

Avinash,

The delete statement will simply remove values based on endda alone when we have non-zero / non-equivalent values among the fields (seqnr & subty). Please look at the below code.I am simply simulating an issue that i faced in my application.

REPORT ytest_sort.

DATA:li_p9 TYPE TABLE OF pa0009 WITH HEADER LINE.

li_p9-begda = '20090515'.

li_p9-endda = '20090520'.

li_p9-subty = '0'.

APPEND li_p9.

li_p9-begda = '20090521'.

li_p9-endda = '99991231'.

li_p9-subty = '0'.

APPEND li_p9.

li_p9-begda = '20090515'.

li_p9-endda = '20090520'.

li_p9-subty = '0'.

li_p9-seqnr = '001'.

APPEND li_p9.

li_p9-begda = '20090521'.

li_p9-endda = '99991231'.

li_p9-subty = '0'.

li_p9-seqnr = '001'.

APPEND li_p9.

li_p9-begda = '20090515'.

li_p9-endda = '20090520'.

li_p9-subty = '1'.

li_p9-seqnr = '001'.

APPEND li_p9.

li_p9-begda = '20090521'.

li_p9-endda = '99991231'.

li_p9-subty = '1'.

li_p9-seqnr = '001'.

APPEND li_p9.

SORT li_p9 BY endda DESCENDING subty seqnr .

DELETE ADJACENT DUPLICATES FROM li_p9 COMPARING subty seqnr.

WRITE 'sorted'.

I hope i have made myself clear here.

Regards,

Kiran

Read only

0 Likes
1,196

Hi,

If you want to delete duplicate entries then its always better to specify all the fields in the delete statement with which the internal table was earlier sorted.

Read only

Former Member
0 Likes
1,196

Hi ,

After each field you need to specify the DECENDING key word otherwise

the table will be sorted by that field in ASCENDING order (which is default).

I have modified a bit your code and now it is working fine.

Please check the code - -

DATA:li_p9 TYPE TABLE OF pa0009 WITH HEADER LINE.

li_p9-begda = '20090515'.
li_p9-endda = '20090520'.
li_p9-subty = '0'.
APPEND li_p9.
li_p9-begda = '20090521'.
li_p9-endda = '99991231'.
li_p9-subty = '0'.
APPEND li_p9.
SORT li_p9 BY endda DESCENDING 
                       subty DESCENDING 
                        seqnr DESCENDING. " added DESCENDING after each field
DELETE ADJACENT DUPLICATES FROM li_p9 COMPARING subty seqnr.

Regards

Pinaki

Read only

Former Member
0 Likes
1,196

Dear all,

Thanks alot for your input.

The sort statement should have been

SORT li_p9 BY subty DESCENDING seqnr DESCENDING endda DESCENDING.

Have granted points to all.

Thanks & Regards,

Kiran