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 while sorting internal table

Former Member
0 Likes
4,644

Hello,

For creating sales order we have created one custom BAPI  and inside this custom BAPI we are calling standard BAPI "BAPI_SALESORDER_CREATEFROMDAT2" to create sales order.

Inside our custom BAPI we have written below SORT statement on table ORDER_ITEM_LINE whose table type is BAPISDITM:

SORT order_item_line BY itm_number.

I am facing issue when the number of records in table ORDER_ITEM_LINE is equal to more than 22 and content of ITM_NUMBER field for all 22 records is INITIAL.

Below is the ORDER_ITEM_LINE content screenshot, before the execution of SORT statement. Here we can see that field ITM_NUMBER content is INITIAL for all 22 records, even next field HG_LV_ITEM content is INITIAL & field PO_ITM_NO is having some values:

Below is the ORDER_ITEM_LINE content screenshot after the execution of SORT statement. Here we can see that even though we have sorted table ORDER_ITEM_LINE by field ITM_NUMBER, system sorted the table in exact reverse order(please see content of field PO_ITM_NO which got reversed sorted):

Please note that if the number of records inside table ORDER_ITEM_LINE is less than or equal to 21, then system is not sorting the table. System is sorting table only when number of records is more than or equal to 22 records.

It would be very helpful, if you can help me to know why system is behaving like this when number of records is more than 22.

Thanks & Regards,

Pradip Ojha

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,176

The answer lies in the Online help for the SORT command.

The Sort command is not a stable sort.  In other words (like SAP's),  the relative order of rows that have the same key is not retained.  In order to retain the order of the rows,  add the STABLE clause to your sort command.

Please refer to the online help for Abap Statements before posting here as very often your answer can be found there.

Rich

3 REPLIES 3
Read only

Patrick_vN
Active Contributor
0 Likes
2,176

Not sure of the how and why the table sorts on column PO_ITM_NO, but your reasoning seems a little awkward to me.

You say the table gets sorted in exact reverse order. Yet your syntax only sorts on the column ITM_NUMBER, and as all values are initial, I would say that the sort result corresponds to the syntax.

Basically, if you want to order a table by several columns, mention all of them in the syntax..

Read only

Former Member
0 Likes
2,177

The answer lies in the Online help for the SORT command.

The Sort command is not a stable sort.  In other words (like SAP's),  the relative order of rows that have the same key is not retained.  In order to retain the order of the rows,  add the STABLE clause to your sort command.

Please refer to the online help for Abap Statements before posting here as very often your answer can be found there.

Rich

Read only

0 Likes
2,176

HI Richard,

Thanks for your help, my issue is now solve. I will make sure from next time to refer online help.

Thanks & Regards,

Pradip Ojha