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

regarding Sorting

Former Member
0 Likes
830

hi friends,

I want to sort a internal table by two or more fields.Mentioning those fields dynamically.

Thanks all,

Harikrishna Adipuram.

7 REPLIES 7
Read only

Former Member
0 Likes
797

Hi ,

<b>SORT</b>
Addition 1 
... BY f1 f2 ... fn 

Effect 
Uses the sort key defined by the sub-fields f1, f2, ..., fn of the table itab instead of the table key. The fields can be of any type; even number fields and tables are allowed. 

You can also specify the sort fields dynamically in the form (name). If name is blank at runtime, the sort field is ignored. If itab is a table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. A field symbol that is not assigned is ignored. If a field symbol is assigned, but does not point to the header line of the internal table, a runtime error occurs.

regards

Prabhu

Read only

naimesh_patel
Active Contributor
0 Likes
797

Hello,

Use this code.

Regards,

Naimesh

REPORT ZTEST_NP.

data: begin of itab occurs 0,

f1,

f2,

f3,

end of itab.

DATA: BEGIN OF IT_SORT OCCURS 0,

FLD(10),

END OF IT_SORT.

ITAB-F1 = 6.

ITAB-F2 = 5.

ITAB-F3 = 4.

APPEND ITAB.

ITAB-F1 = 1.

ITAB-F2 = 2.

ITAB-F3 = 3.

APPEND ITAB.

IT_SORT = 'F1'.

APPEND IT_SORT.

IT_SORT = 'F2'.

APPEND IT_SORT.

IT_SORT = 'F3'.

APPEND IT_SORT.

sort itab by (IT_SORT).

LOOP AT ITAB.

WRITE: / ITAB.

ENDLOOP.

Read only

0 Likes
797

hi naimesh,

if i mention like this ,it will sort by standard key.

I already try this one

Thanks,

Harikrishna

Read only

dani_mn
Active Contributor
0 Likes
797

Hi,

Use field symbols to do it.

FIELD-SYMBOLS: <FS> type any.

FIELD-SYMBOLS: <FS1> type any.

ASSIGN field_name to <FS>.

ASSIGN field_name1 to <FS1>.

SORT ITAB by <FS> <FS1>.

Regards,

Wasim Ahmed

Read only

nishanthbhandar
Contributor
0 Likes
797

You have to make use of field symbols to sort the internal table dynamically.

declare the field symbols first of type ANY.

field-symbols : <f1> type any,

<f2> type any.

Assign the fields to the field symbols.

assign dynamic-field1 to <f1>.

assign dynamic-field2 to <f2>.

Then sort dynamically using the below syntax.

sort ITAB by (<f1>) (<f2>).

Message was edited by: Nishanth Bhandar

Read only

0 Likes
797

i want use one dynamic varible to sort the internal table by two or more fields.

Thanks all,

Harirkishna adipuram

Read only

Former Member
0 Likes
797

Hi,

Consider this code.


REPORT zztest_arun_2.

PARAMETERS p_matnr TYPE mara-matnr.
DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.

DATA : field1 TYPE string,
       field2 TYPE string.

START-OF-SELECTION.

  SELECT * FROM mara INTO TABLE it_mara UP TO 200 ROWS WHERE matnr = p_matnr.

  field1 = 'MATNR'.
  field2 = 'MTART'.
  SORT it_mara BY (field1) (field2).


Regards,

Arun Sambargi.