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 syntax

Former Member
0 Likes
1,883

Hi Experts,

I am facing a strange case which I would like to clarify with experts.

There is an internal table in my report where I am sorting the internal table using the field Bukrs,Belnr,Gjahr. (Ascending order)

Case 1 :

A document in that internal table contains 2 line item as given below.

Bukrs YearBelnrAmount
1002012101000
1002012101-100.00

Now when I am sorting this internal table by ascending order, then the values in the internal table remains same.

(Note : The value remains same when the internal table contains minimum documents Ex : 10 documents)

Case 2

When the same internal table sorted with the same code, the values in the internal table getting swapped as given below, incase the internal table contains huge number of document (EX: 1000 documents)

Bukrs YearBelnrAmount
1002012101-100.00
1002012101000

My requirement is that the values in the internal table should not swap when I am sorting.

Kindly share your views for the same

Best Regards,

Raja.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,664

Look for SORT statement syntax via F1 or online help (look for STABLE option)

Regards,

Raymond

14 REPLIES 14
Read only

Former Member
0 Likes
1,664

Raja,

you need to have 1 more field in sorting ( add  new field rec_no ) populate it with record no & use it in sorting which will make sure the all the time your records for same bukrs , belnr & gjahr in same order.

Raj Patel

Read only

0 Likes
1,664

Yes he could add field BUZEI, but this is not required.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,665

Look for SORT statement syntax via F1 or online help (look for STABLE option)

Regards,

Raymond

Read only

0 Likes
1,664

Thanks Raymond

i agree he should use STABLE option instead of adding new field in table.

Read only

0 Likes
1,664

Hi Raymond,

Thanks for your help!

However I have one doubt, If I am using stable, the values will remains same if the 2 lineitem is belongs to the same document.

 

Requirement : The highest amount of the same document always need to be in the last line.Say a document contains 3 line, then the highest amount line item need to be in the 3rd line. This particular sort is working fine if we add "Amount" fields as a key field when all line item has positive values.

The same sort is not working if a document has several line items with negative values in one line item as well as 0 value in another line item. Say a document has 3 lines, 1st line has "00" value in amount field, 2nd line has "-50" and 3r line has "-100". Then the amount -100 need to be in last line. However when I am sorting the internal table using ascending, since the value "00" is greater than

"-100", the value "00" is coming in the last line. But as per my requirement it should not consider the Signs (+/-), it needs to consider only values.

Best Regards,

Raja

Read only

0 Likes
1,664

The STABLE option will insure that the two records with same key values will stay in the same order in the table.

You could have kept the original field (DMBTR, WRBTR, etc. from BSEG) which usually has no sign (sign is calculated from BSEG-SHKZG) and add another column with the signed amount (DMSHB, WRSHB, etc. from BSEGA) hide the not-signed field in the report, but use it a a sort  criteria.

Using STABLE option, you only have to sort by absolute value once, then the records will keep the same relative order in the document.

So you could first fill the internal table with not-signed values, SORT the table, then update the amount using SHKZG.

Regards

Raymond

Read only

0 Likes
1,664

Thanks Raymond,

Ponits rewarded.

Please explain how the STABLE sort will work.

It would be really helpful if you explain through an example program.

Best Regards,

Raja.

Read only

0 Likes
1,664

HI Raja,

Check the program "DEMO_INT_TABLES_SORT_STABLE" in SE38.

you can debug & understand the working.

Thanks,

Sharath

Read only

0 Likes
1,664

Hi Sharath,

Thanks for ur help!

The program  "DEMO_INT_TABLES_SORT_STABLE" is from ABAPDOCU?

Best Regards,

Raja

Read only

0 Likes
1,664

Yes it is from ABAPDOCU

Read only

0 Likes
1,664

Hi Sharath,

It will be really helpful if you share some available demo objects(Report, etc..)which may help beginners to learn SAP in right way.

Thanks & Best Regards,

Raja

Read only

0 Likes
1,664

Hi Sharath,

It will be really helpful if you share some available demo objects(Report, etc..)which may help beginners to learn SAP in right way.

Thanks & Best Regards,

Raja

Read only

0 Likes
1,664

Hi Raja,

One more link for your reference:-

http://wiki.sdn.sap.com/wiki/display/Snippets/Difference+Between+SORT+and+SORT+STABLE

Thanks,

Sharath

Read only

0 Likes
1,664

Hi Sharath,

Points rewarded!

Really helpful.

Thanks and Best Regards,

Raja