Application Development 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: 

Dynamic sort ascending / descending

Former Member
0 Kudos

Hi all,

I am trying to sort an internal table dynamically.

In this table I have a number of field to use for an ascending sort and other used to sort descending.

I have writen the code to create two fields (character field) in which I concatenate the sort field names.

The result is :

sort_field_up = ' field1 field2 field 3'

sort_field_dn = ' field4 field5 field 6'

and then i coded :

sort itab by (sort_field_up) ascending (sort_field_dn) descending.

But I get a dump ...

has anyone ever done this ?

regards,

Nicolas

6 REPLIES 6

vinod_gunaware2
Active Contributor
0 Kudos
  • Dynamic Sort of internal TAable

tables : mara.

data : itab like mara occurs 0 with header line.

data : begin of fieldtab occurs 0 ,

name(40),

end of fieldtab.

select * from mara into table itab up to 100 rows.

fieldtab-name = 'ERSDA'.

append fieldtab.

fieldtab-name = 'ERNAM'.

append fieldtab.

break-point.

sort itab by (fieldtab).

break-point.

regards

vinod

Former Member
0 Kudos

hi Nicholas,

try using two sort statements i.e, 1 sort for ascending and other as a decending sort ...

i.e, <b>sort itab by (sort_field_up) ascending .

sort itab by (sort_field_dn) descending.</b>

or else

<b> sort itab by field1 field2 field 3 ascending.

sort itab by field4 field5 field 6 descending.</b>

Regards,

Santosh

0 Kudos

go through this link ur problem wiil be solved...

Former Member
0 Kudos

Hi,

I guess u can use :

<b>sort itab by field1 field2 field 3 ascending.</b>

<b>sort itab by field4 field5 field 6 descending.</b>

in two different steps.

This may give you the same result.

Thanks,

Rashmi.

naimesh_patel
Active Contributor
0 Kudos

Hello,

Creat an internal table an fill your fields <b>IN CAPITAL LETTERS</b> into that and then do sort.

Regards,

Naimesh

former_member188685
Active Contributor
0 Kudos

Hi,

only single fields can be mentioned in dynamic sort.

REPORT  Z_DYNAMIC_SORT                          .

data: sort1 type string,
      sort2 type string.
data: begin of itab occurs 0,
       vbeln like vbak-vbeln,
       posnr like vbap-posnr,
      end of itab.
"not possible to group the fileds.
"sort_field_up = ' field1 field2 field 3'
 sort1 = 'VBELN'.
 sort2 = 'POSNR'.
sort itab by (sort1) ascending (SORT2) descending.
"or 
sort itab by (sort1) (SORT2) ascending .
write sy-subrc.

regards

vijay