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

Sort internal table by using field symbol

Former Member
0 Likes
2,254

Hi All,

I encountered the runtime error while try to sort the internal table by using field symbol below. Can anyone please advice how does this issues can be resolved? Thanks.

SORT i_test

BY <fs_a> ASCENDING <fs_b>.

Runtime error: -

When attempting to sort the internal table "I_test[ ]" in the current ABAP/4

program "Ztest", the field symbol " " was used as a dynamic

criterion.

To be used in this way, all of " " would have to fit into the header

line "I_test" of the internal table "I_test[ ]".

Since this is not the case, the program "Ztest" had to be terminated.

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,312

If the field-symbols <fs_a> & <fs_b> contain the you can try:

SORT i_test
BY ( <fs_a> ) ASCENDING ( <fs_b> ).

Check this online help documentation for details [http://help.sap.com/abapdocu_70/en/ABENITAB_COMPONENTS.htm], [http://help.sap.com/abapdocu_70/en/ABAPSORT_ITAB.htm#!ABAP_ADDITION_4@4@]

BR,

Suhas

If the field-symbols <fs_a> & <fs_b> contain the you can try:

SORT i_test
BY ( <fs_a> ) ASCENDING ( <fs_b> ).

Check this online help documentation for details [http://help.sap.com/abapdocu_70/en/ABENITAB_COMPONENTS.htm], [http://help.sap.com/abapdocu_70/en/ABAPSORT_ITAB.htm#!ABAP_ADDITION_4@4@]

BR,

Suhas

3 REPLIES 3
Read only

Former Member
0 Likes
1,312

You can also specify the sort fields dynamically in the form (name). If name is blank at runtime, the sort field is ignored. If itab is a table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. A field symbol that is not assigned is ignored. If a field symbol is assigned, but does not point to the header line of the internal table, a runtime error occurs.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,313

If the field-symbols <fs_a> & <fs_b> contain the you can try:

SORT i_test
BY ( <fs_a> ) ASCENDING ( <fs_b> ).

Check this online help documentation for details [http://help.sap.com/abapdocu_70/en/ABENITAB_COMPONENTS.htm], [http://help.sap.com/abapdocu_70/en/ABAPSORT_ITAB.htm#!ABAP_ADDITION_4@4@]

BR,

Suhas

Read only

Former Member
0 Likes
1,312

Hi All,

Sort with field symbol for an internal table is not allowed within an loop.

Please see the following code and it is working:

TYPES : BEGIN OF y_trial,

n TYPE i,

END OF y_trial.

DATA : t_table TYPE STANDARD TABLE OF y_trial WITH HEADER LINE,

e_table LIKE LINE OF t_table.

FIELD-SYMBOLS : <fs_trial> TYPE y_trial.

INITIALIZATION.

CLEAR e_table.

MOVE 10 to e_table-n.

APPEND e_table to t_table.

CLEAR e_table.

MOVE 15 to e_table-n.

APPEND e_table to t_table.

CLEAR e_table.

MOVE 7 to e_table-n.

APPEND e_table to t_table.

" Assign the field symbol to internal table and then sort.

ASSIGN t_table to <fs_trial>.

SORT t_table ASCENDING by <fs_trial>.

LOOP AT t_table ASSIGNING <fs_trial>.

WRITE 😕 <fs_trial>-n.

ENDLOOP.

Output is : 7,10,15.