2012 Jun 07 2:38 PM
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.
2012 Jun 07 2:44 PM
2012 Jun 07 2:55 PM
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.
2012 Jun 07 3:21 PM
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).
2012 Jun 07 3:22 PM
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.
2012 Jun 07 3:31 PM
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
2012 Jun 07 2:50 PM
2012 Jun 07 2:53 PM
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
2012 Jun 07 5:40 PM
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