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 in alv

Former Member
0 Likes
629

Hi all,

how to sort two alv fields by comparing with the first field..

field1 field2

1 a

1 a

1 b

2 c

3 c

i want to dispaly the output like the below

field1 field2

1 a

b

2 c

3 c

when i try the below code

wa_sort-spos = 1.

wa_sort-fieldname = 'FIELD1'.

append wa_sort to it_sort.

clear wa_sort.

wa_sort-spos = 2.

wa_sort-fieldname = 'FIELD2'.

append wa_sort to it_sort.

clear wa_sort.

the output is coming like this

field1 field2

1 a

b

2 c

3 -->c is merging for both 2 and 3

pls let me know how to rectify this..

Hi all,

how to sort two alv fields by comparing with the first field..

field1 field2

1 a

1 a

1 b

2 c

3 c

i want to dispaly the output like the below

field1 field2

1 a

b

2 c

3 c

when i try the below code

wa_sort-spos = 1.

wa_sort-fieldname = 'FIELD1'.

append wa_sort to it_sort.

clear wa_sort.

wa_sort-spos = 2.

wa_sort-fieldname = 'FIELD2'.

append wa_sort to it_sort.

clear wa_sort.

the output is coming like this

field1 field2

1 a

b

2 c

3 -->c is merging for both 2 and 3

pls let me know how to rectify this..

4 REPLIES 4
Read only

Former Member
0 Likes
583

Its simple

for field 1 and field 2

pass sort_up = 'X'.

hope it helps

bhanu

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
583

Hi,

Try this:-


*SORTING
DATA : it_sort TYPE slis_t_sortinfo_alv,
       wa_sort TYPE slis_sortinfo_alv.

*&---------------------------------------------------------------------*
*          SORT W.R.T. FIELD1
*&---------------------------------------------------------------------*
  wa_sort-spos = 1.
  wa_sort-fieldname = '<FIELD1>'.
  wa_sort-tabname = 'ITAB'.
  wa_sort-up = 'X'.
  APPEND wa_sort TO it_sort.
  CLEAR wa_sort.

"now pass this it_sort in REUSE_ALV_GRID_DISPLAY

Hope this helps you.

Regards,

Tarun

Read only

Former Member
0 Likes
583

Hi,

Instead of declaring the field catalog for sorting you can simply sort your internal table as follows:

SORT <i_tab> BY <field1> ASCENDING <field2> ASCENDING.

Regards.

Edited by: rajan roy on Mar 5, 2009 1:30 PM

Read only

Former Member
0 Likes
583

Hi Mallika,

Before passing the table for display ,


DELETE adjacent duplicates from t_itab comparing all fields.
describe table t_itab lines w_lines.
loop at t_itab.
  w_fld1 = t_itab-fld1.
  w_idx = sy-tabix + 1.
  do w_lines times.
  read table t_itab index w_idx.
  if t_itab-fld1 = w_fld1.
     t_itab-fld1 = space.
    endif.
   add 1 to w_idx.
   if w_idx = w_lines.
   exit.
   endif.
  enddo.
  endloop.

  fs_fcat-fieldname = 'FLD1'.
  append fs_fcat to t_fcat.

    fs_fcat-fieldname = 'FLD2'.
    append fs_fcat to t_fcat.


  fs_sort-spos = 1.
  fs_sort-fieldname = 'FLD1'.
  fs_sort-UP = 'X'.
  append fs_sort to t_sort.

Regards,

Mdi.Deeba