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

sort internal table

Former Member
0 Likes
896

Hi all,

I have output data like.

belnr wrbtr1 wrbtr2 wrbtr3

89990 290

89990 234

89990 897

89991 342

89991 586

89991 562

But need output like thes.

belnr wrbtr1 wrbtr2 wrbtr3

89990 290 234 897

89991 342 586 562

1 ACCEPTED SOLUTION
Read only

former_member404244
Active Contributor
0 Likes
873

Hi,

Try like this

data : v_index type i.

v_index = 2.

loop at itab into wa_tab.

read table itab into wa_tab1 index v_index.

if wa_tab1-belnr = wa_tab-belnr and wa_tab-wrbtr2 is initial.

move : wa_tab1-wrbtr1 to wa_tab-wrbtr2.

modify itab from wa_tab index 1.

clear : wa_tab,

wa_tab1.

elseif wa_tab1-belnr = wa_tab-belnr and wa_tab-wrbtr3 is initial.

move : wa_tab1-wrbtr1 to wa_tab-wrbtr3.

modify itab from wa_tab index 1.

clear : wa_tab,

wa_tab1.

endif.

v_index = v_index + 1.

endloop.

Regards,

Nagaraj

7 REPLIES 7
Read only

Former Member
0 Likes
873

hi,

You can use ALV list or grid for this. or use the write statement in such a way to appear like this. Iys better to use ALV.

Hope this helps u.

Regards,

Arunsri

Read only

Former Member
0 Likes
873

Hi,

SORT for Internal Tables

Sorts internal tables.

Syntax

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE]

... BY <fi> [ASCENDING|DESCENDING] [AS TEXT]...

Sorts the internal table <itab>. If you omit the BY addition, the table is sorted by its key. You can define a different sort key by using the BY addition. The other additions specify whether you want to sort in ascending or descending order, and whether strings should be sorted alphabetically.

It might be useful.

Regards,

Priya.

Read only

former_member404244
Active Contributor
0 Likes
874

Hi,

Try like this

data : v_index type i.

v_index = 2.

loop at itab into wa_tab.

read table itab into wa_tab1 index v_index.

if wa_tab1-belnr = wa_tab-belnr and wa_tab-wrbtr2 is initial.

move : wa_tab1-wrbtr1 to wa_tab-wrbtr2.

modify itab from wa_tab index 1.

clear : wa_tab,

wa_tab1.

elseif wa_tab1-belnr = wa_tab-belnr and wa_tab-wrbtr3 is initial.

move : wa_tab1-wrbtr1 to wa_tab-wrbtr3.

modify itab from wa_tab index 1.

clear : wa_tab,

wa_tab1.

endif.

v_index = v_index + 1.

endloop.

Regards,

Nagaraj

Read only

Former Member
0 Likes
873

Ankita,

Take one internal table which should have your required structure.

DATA : v_temp TYPE I,

v_cnt TYPE I .

LOOP AT ITAB.

v_temp = v_temp + 1.

ON CHANGE OF ITAB-belnr

v_cnt = v_cnt + 1.

MOVE : ITAB-belnr To i_final-belnr.

ITAB-wrbtr1 TO i_final-wrbtr1.

APPEN I_final.

CLEAr i_final.

ENDON.

IF v_temp Eq 2.

MOVE : ITAB-wrbtr1 TO i_final-wrbtr2.

INSERT i_FINAL INDEX v_CNT

ELSEIF v_temp Eq 3

MOVE : ITAB-wrbtr1 TO i_final-wrbtr3.

INSERT i_FINAL INDEX v_CNT

CLEAR v_temp.

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
873

Hi,

Please try the following,

Suppose your internal table contains your fields as mentioned.

Please use

SORT <internal_table> DESCENDING BY BELNR WRBTR1 WRBTR2 WRBTR3.

Reward if Helpful.

Read only

Former Member
0 Likes
873

Hi,

try like dis.....

DATA c.
LOOP AT itab.
  c = c + 1.
  CASE c.
    WHEN 2.
      itab-wrbtr2 = itab-wrbtr1.
    WHEN 3.
      itab-wrbtr3 = itab-wrbtr1.
      AT END OF belnr.
        APPEND itab.
        CLEAR c.
      ENDAT.
  ENDCASE.
ENDLOOP.

LOOP AT itab WHERE itab-wrbtr2 IS INITIAL
             AND   itab-wrbtr3 IS INITIAL.
  DELETE itab.
ENDLOOP.

Cheers,

jose.

Read only

Former Member
0 Likes
873

Hi,

Sort itab_final by belnr ASCENDING.

Regds