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!

Former Member
0 Likes
857

Hi I have an internal table and using a report i list all the fields of the internal table. Now I have a sort button in the pf-status. when i click this button it asks for three fields to sort from. So how do i sort the internal table by using the fields given by the user in the slection screen. Thanks

Vicky

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
768

hi,

this is the info copied from help.sap.com.

yes, we can give the sorting field names dynamically at runtime. here is the way.

for further help,refer,

<a href="http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm">SAP Help</a>

<b>explanation :</b>

you have an internal table with a structured line type that you want sort by a different key, you can specify the key in the SORT statement:

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]

BY <f1> [ASCENDING|DESCENDING] [AS TEXT]

...

<fn> [ASCENDING|DESCENDING] [AS TEXT].

The table is now sorted by the specified components <f 1 > ... <f n > instead of by the table key. The number of sort fields is limited to 250. The sort order depends on the sequence of the fields <f i >. The sort sequence specified before BY applies to all fields. The sort sequence after a field applies only to that column of the table.

You can specify a sort field dynamically by specifying (<f>) instead of <f i >. The contents of the field <f> determines the name of the sort field. If <f> is empty when the statement is executed, the field is ignored in the sort. If it contains an invalid component name, a runtime error occurs.

7 REPLIES 7
Read only

Former Member
0 Likes
768

Hi Vicky Vicky!

...sort internaltablename by field1 field2...

Can you not just sort the itab and then redisplay? If that's not what you meant, please ask again.

Hope this helps.

Regards,

Den

Read only

Former Member
0 Likes
768

You have to dynamically assign the field name chosen by the user to a variable and use that in your sort.


data: v_fieldname like dd03l-fieldname.

move user_choice to v_fieldname.

sort itab by (v_user_choice).

Read only

Former Member
0 Likes
768

And... maybe you'll need to use field-symbols. Consult the help page for this in your abap program or refer to

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/frameset.htm

Again, hope this helps.

Regards,

Deni

Read only

Former Member
0 Likes
769

hi,

this is the info copied from help.sap.com.

yes, we can give the sorting field names dynamically at runtime. here is the way.

for further help,refer,

<a href="http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm">SAP Help</a>

<b>explanation :</b>

you have an internal table with a structured line type that you want sort by a different key, you can specify the key in the SORT statement:

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]

BY <f1> [ASCENDING|DESCENDING] [AS TEXT]

...

<fn> [ASCENDING|DESCENDING] [AS TEXT].

The table is now sorted by the specified components <f 1 > ... <f n > instead of by the table key. The number of sort fields is limited to 250. The sort order depends on the sequence of the fields <f i >. The sort sequence specified before BY applies to all fields. The sort sequence after a field applies only to that column of the table.

You can specify a sort field dynamically by specifying (<f>) instead of <f i >. The contents of the field <f> determines the name of the sort field. If <f> is empty when the statement is executed, the field is ignored in the sort. If it contains an invalid component name, a runtime error occurs.

Read only

Former Member
0 Likes
768

You have to read the report back in, evaluate the fields and sort the table based on those results.

Rob

Read only

0 Likes
768

When the user presses the button to do the sort, is he or she presented with a screen that has the fields and a number for a sort sequence? If not, what is on the screen?

Rob

Read only

Former Member
0 Likes
768

it seems that you want to sort the data prior to displaying on the screen.

that's easy - just sort itab by parameter1 parameter2 parameter3 as mentioned by Den.

you can also remove that sort button from your PF status so the users don't get confused.