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 with variable no of fields

Former Member
0 Likes
1,984

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

6 REPLIES 6
Read only

Former Member
0 Likes
1,172

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

Read only

Former Member
0 Likes
1,172

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

Read only

Former Member
0 Likes
1,172

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

Read only

Former Member
0 Likes
1,172

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

Read only

Former Member
0 Likes
1,172

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.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,172

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.