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

Sorting internal table dynamically.

Former Member
0 Likes
5,971

Hi All,

I want to sort an internal table dynamically. That is the sorting of the table will be done on different no of filelds, ascending, descending based on the user interaction.

The detailed issue is:

I have an ALV grid display of a dynamic structure.

This displayed data is then sorted and filtered by the  user.

This sorting is not getting applied for the internal table holding the data.

So i have to sort and filter the table in my program.

Filtering part is done.

Sorting I am stuck.

dynamic sort using single field is working, but when there are multiple fields, it is not possible.

I cant sort one by on as the sorting by the first field will be lost.

8 REPLIES 8
Read only

RaymondGiuseppi
Active Contributor
0 Likes
3,519

Try to sort field by field, from last key to first key with a STABLE option.

Regards,

Raymond

Read only

0 Likes
3,519

hello,

I think sorting is not possible by using field symbols, it can be used to modify table contents. This is because field symbol will just point to a line of the table.

if you want your table to be sorted in order of its key fields just use the statement.

       <b> SORT <itab> [ASCENDING|DESCENDING] </b>

Variable number of fields will not cause any problem in this.

But if you want table to sorted by a different key, you can specify the key in the SORT statement. There you need to know field names (either static or dynamically) .

<b>

SORT <itab> [ASCENDING|DESCENDING]

             BY <f1> [ASCENDING|DESCENDING]

                    ...

                   <fn> [ASCENDING|DESCENDING]</b>

f1, f2, ........fn are the fields according to which you want to sort.

Regards,

Namitha.

Read only

0 Likes
3,519

Hi Namitha..

SOR and the internal table are the only thins are know..

others are dynamic..

that is whether ascending or descending and the field names..

I tried somthing like SORT itab direction  (variable name holding the field name).

Read only

0 Likes
3,519

Hi Raymond, can you please elaborate what is STABLE and how do I decide the key field?

as the fields on which I want the sort are dynamic.

Read only

0 Likes
3,519

Look at the sample at the end of SORT itab., a syntax like (where otab is of TYPE abap_sortorder_tab)

SORT <itab> BY (otab). 

seems to be allowed

NB: The STABLE options insures that the relative order of records with the same sort criteria does not change.

Regards,

Raymond


Read only

Former Member
0 Likes
3,519

Hi,

Have you try using the addition BY (otab)?

Kr,

Manu.

Read only

Former Member
0 Likes
3,519

You can use this.

 

DATA: l_otab TYPE abap_sortorder_tab,

            l_oline TYPE abap_sortorder.

LOOP AT gt_fld ASSIGNING <l_column>. " GT_FLD has the dynamic sort fields

l_oline-name = <l_column>-fldnam. " Add column name

APPEND l_oline TO l_otab.

ENDLOOP.

SORT gt_alv_temp BY (l_otab).

Thanks,

Shambu

Read only

Subhankar
Active Contributor
0 Likes
3,519

Hi Piyush,

You can sort the table dynamically on many fields..

Sort <ITAB> by ('FIELD1') ASCENDING ('FIELD2') DESCENDING..........

you can also provide the field name in variables like below.

Sort <ITAB> by (VAR1) ASCENDING (VAR2) DESCENDING...........

here vari1 and var2 store the name of the field.

When you do some dynamic sort better to provide TRY and CATCH block. Other wise if there any typo or incorrect field name then it will give dumps. Catch the exception CX_SY_DYN_TABLE_ILL_COMP_VAL.

Thanks

Subhankar