2007 May 29 1:21 PM
Hi ,
I want to sort the internal table with variable no of fields . Every time the report is run the number of fields as well as the sequence by which the table has to be sorted changes . How to do this .
regards
Hi ,
I want to sort the internal table with variable no of fields . Every time the report is run the number of fields as well as the sequence by which the table has to be sorted changes . How to do this .
regards
2007 May 29 1:27 PM
Hi,
Use the Field Symbols for thisone, pass the value which you want to sort the Internal table, then use this Filed-Symbol to srot the Internal table
Regards
Sudheer
2007 May 29 1:28 PM
Hi,
Please try this.
DATA: BEGIN OF ITAB OCCURS 0,
F1(4),
END OF ITAB.
DATA: BEGIN OF ITAB2 OCCURS 0,
F1(1),
F2(1),
F3(4),
END OF ITAB2.
ITAB-F1 = '01AC'.
APPEND ITAB.
ITAB-F1 = '02AB'.
APPEND ITAB.
ITAB-F1 = '01CD'.
APPEND ITAB.
ITAB-F1 = '02CA'.
APPEND ITAB.
LOOP AT ITAB.
ITAB2-F1 = ITAB-F1+2(1).
ITAB2-F2 = ITAB-F1+3(1).
ITAB2-F3 = ITAB-F1.
APPEND ITAB2.
ENDLOOP.
SORT ITAB2 BY F1 ASCENDING
F2 DESCENDING.
LOOP AT ITAB2.
WRITE: / ITAB2-F3.
ENDLOOP.
REWARD POINTS IF HELPFUL
2007 May 29 1:30 PM
THEN ...
sort the internal table .....
example
sort itab ..... it will sort the internal table by fields index like first declared will sorted first ... then second ... thrid ... n ...etc..
if you want to sort any specific range then go for
example
sort itab by first third fourth second etc ..
girish
2007 May 29 1:32 PM
Hi
U can use a number of max fields for sorting:
DATA: FIELD1(30),
FIELD2(30),
FIELD3(30).
SORT ITAB BY (FIELD1) (FIELD2) (FIELD3).
Max
2007 May 29 1:33 PM
Hello Neelesh,
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.
Anyways as far as your requirement is concerned 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. It can be harcoded or set dynamically.
Award points if useful.
Regards
Indrajit.
2007 May 29 2:05 PM
HI,
you have to put your fields in an i_tab then use
sort tablename by ( i_tab ).
Here is an example:
PARAMETERS dbtab TYPE c LENGTH 30.
SELECT-OPTIONS columns FOR dbtab NO INTERVALS.
DATA: otab TYPE abap_sortorder_tab,
oline TYPE abap_sortorder,
dref TYPE REF TO data.
FIELD-SYMBOLS: <column> LIKE LINE OF columns,
<itab> TYPE STANDARD TABLE.
TRY.
CREATE DATA dref TYPE STANDARD TABLE OF (dbtab).
ASSIGN dref->* TO <itab>.
CATCH cx_sy_create_data_error.
MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
TRY.
SELECT *
FROM (dbtab)
INTO TABLE <itab>.
CATCH cx_sy_dynamic_osql_semantics.
MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
LOOP AT columns ASSIGNING <column>.
oline-name = <column>-low.
APPEND oline TO otab.
ENDLOOP.
TRY.
SORT <itab> BY (otab).
CATCH cx_sy_dyn_table_ill_comp_val.
MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.
Regards,
Sooness.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |