1

Even though I am sorting the internal table LT_DATA by the FKDAT field, the POSNR field is still being sorted in an unexpected order. For example, in the original data of the internal table, POSNR value '1' appears first, followed by POSNR value '2'. However, after sorting by FKDAT, the order of POSNR changes as well, which I didn’t expect. Please refer to the highlighted section of the image for clarification.

I'm trying to understand the internal logic behind how the sorting is being handled.

When I modify the code to: SORT lt_data BY fkdat vbeln posnr.

I get the desired output.

Can you help explain why POSNR is being sorted in the first case, and how the sorting logic works internally?

TYPES: BEGIN OF lty_data,
         fkdat TYPE vbrk-fkdat,
         vbeln TYPE vbrk-vbeln,
         posnr TYPE vbrp-posnr,
       END OF lty_data.`

DATA: lt_data TYPE STANDARD TABLE OF lty_data.


lt_data = VALUE #( ( vbeln = '1203002977' posnr = '000001' fkdat = '20240407' )
                   ( vbeln = '1203002977' posnr = '000002' fkdat = '20240407' )
                   ( vbeln = '1203002977' posnr = '000003' fkdat = '20240407' )
                   ( vbeln = '1203002977' posnr = '000004' fkdat = '20240407' )
                   ( vbeln = '1203003080' posnr = '000001' fkdat = '20240515' )
                   ( vbeln = '1203003080' posnr = '000002' fkdat = '20240515' )
                   ( vbeln = '1203003080' posnr = '000003' fkdat = '20240515' )
                   ( vbeln = '1203003080' posnr = '000004' fkdat = '20240515' )

                   ( vbeln = '1205010163' posnr = '000001' fkdat = '20240406' )
                   ( vbeln = '1205010163' posnr = '000002' fkdat = '20240406' )

                   ( vbeln = '1205010254' posnr = '000001' fkdat = '20240424' )
                   ( vbeln = '1205010254' posnr = '000002' fkdat = '20240424' )
                   ( vbeln = '1205010254' posnr = '000003' fkdat = '20240424' )
                   ( vbeln = '1301012394' posnr = '000001' fkdat = '20240429' )
                   ( vbeln = '1301012394' posnr = '000002' fkdat = '20240429' )
                   ( vbeln = '1323000956' posnr = '000001' fkdat = '20240510' )
                   ( vbeln = '1323000956' posnr = '000002' fkdat = '20240510' )
                   ( vbeln = '1323000967' posnr = '000001' fkdat = '20240513' )
                   ( vbeln = '1323000967' posnr = '000002' fkdat = '20240513' )
                   ( vbeln = '1323000982' posnr = '000003' fkdat = '20240516' )
                   ( vbeln = '1323000982' posnr = '000004' fkdat = '20240516' )
                   ( vbeln = '1323000982' posnr = '000005' fkdat = '20240516' )
                   ( vbeln = '1323001014' posnr = '000001' fkdat = '20240527' )
                   ( vbeln = '1323001025' posnr = '000001' fkdat = '20240606' ) ).

SORT lt_data BY fkdat.

Output:

Output

1
  • As already answered, sorting is "unstable" by default. Avoid using the word STABLE if one column can be used to order the lines, because it's always best to be explicit in the code. Also, I would recommend using SORTED or HASHED tables instead of STANDARD, hence no SORT needed anymore. Commented Apr 2 at 11:02

2 Answers 2

6

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapsort_itab.htm

What you miss:

"Sorting is unstable by default, which means that the relative order of rows that do not have different sort keys is not preserved when they are sorted. The order can be different depending on the platform or when sorted multiple times."

This simply means, if you sort by one field only (fkdat), the rest will be random and could vary.

Sign up to request clarification or add additional context in comments.

1 Comment

Although you've answered the question, I think it will be useful if you add the STABLE thing of Herr Berthm's answer.
3

Sorting isn't stable by default. But you can use the STABLE addition.
This should work:

SORT lt_data STABLE BY fkdat.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.