on 2025 Apr 15 8:13 PM
Hello,
I know this would be basic question, but I need this to figure-out issue. I have an internal table lt_result with four fields (field1, field2, field3, field4). I have fetched data from an Infotype into this internal table. Now, I have N no.of records in lt_result. Among these, I have two records with same value in field2 where field4 is varied.
EG: Before SORT
field1 | field2 | field3 | field4 |
12345 | 01.01.2013 | 01.01.2013 | 01.04.2012 |
12345 | 04.11.2020 | 04.11.2020 | 04.11.2020 |
12345 | 20.12.2022 | 20.12.2022 | 01.04.2012 |
12345 | 20.12.2022 | 20.12.2022 | 04.11.2020 |
I would like to sort with field1 ascending and field2 descending.
SORT lt_result BY <field1> ASCENDING <field2> DESCENDING.
In that case, what will happen if for the records in violet.
i) which record will come first either 01.04.2012 or 04.11.2020?
ii) usually how does sorting works for date comparison?
Thanks!!
Request clarification before answering.
Dear ABAPer_1631,
When you execute this sort command SORT lt_result BY field1 ASCENDING field2 DESCENDING you'll get a follow Table:
Primary key: field1 (ASCENDING)
- All rows have field1 = 12345
- Since every value is identical, the order remains unchanged
Secondary key: field2 (DESCENDING)
- Date fields (TYPE D) are stored internally as an integer in the format YYYYMMDD
- With DESCENDING, the comparison is lexicographic/numeric 'backwards' (newer dates (larger numbers) come before older dates (smaller numbers)).
Stable sort for identical keys
- If two records share the same values in field1 and field2, their original insertion order is preserved.
i) Which record appears first: 01.04.2012 or 04.11.2020?
- Both entries actually share field2 = 20.12.2022.
- The sort criteria are exhausted, so they remain in their original order:
○ Record with field4 = 01.04.2012 (originally row 3)
○ Record with field4 = 04.11.2020 (originally row 4)
-> 01.04.2012 appears before 04.11.2020.
ii) How does the date comparison work?
- Date fields (TYPE D) are stored internally in the format YYYYMMDD.
- Compared lexicographically/numerically For DESCENDING: 20221220 > 20201104 > 20130101 > 20120401
-> Newest dates end up at the top.
I hope my explanation has provided some clarity.
Best regards,
Dennis
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
30 | |
22 | |
16 | |
8 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.