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: 

Sort Internal tbale with fiel;d values

Former Member
0 Kudos

Hi All

I have to sort an internal based on values of field . in the below order of value

CTP

HPL

SPL

ANC.

Wolud like know to if theres a better method of doing than what I did below in an user exit .

DATA : t_ltap_vb_temp TYPE  ltap_vb OCCURS 0 WITH HEADER LINE .

*Need to sort ltap_vb with letyp value in order of 'CTP' , 'HPL' ,'SPL' , 'ANC'
*could not find a better logic

break kollav.
LOOP AT t_ltap_vb WHERE letyp = 'CTP'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  DELETE t_ltap_vb.
ENDLOOP.

LOOP AT t_ltap_vb WHERE letyp = 'HPL'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  DELETE t_ltap_vb.
ENDLOOP.

LOOP AT t_ltap_vb WHERE letyp = 'SPL'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  DELETE t_ltap_vb.
ENDLOOP.

LOOP AT t_ltap_vb WHERE letyp = 'ANC'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  DELETE t_ltap_vb.
ENDLOOP.

LOOP AT t_ltap_vb.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
ENDLOOP.

t_ltap_vb[] = t_ltap_vb_temp[].

Any alternative suggestions will be great .

Thanks

Vinay

Edited by: vinay kolla on May 20, 2009 11:21 AM

9 REPLIES 9

former_member242255
Active Contributor
0 Kudos

hi vinay,

may be by making use of the CASE staement append the records under different category into different internal tables and then use

APPEND LINES to append the records of all interanlt ables in the order what ever you want.

you may be using more number of ointernal tables but i think u can skip looping somany times the same internal table..

Former Member
0 Kudos

Follow these steps to avoid multiple loops.

1. Sort the internal table in ascending order.

2. Loop at internal table. Read at index 1 which could be 'ANC' and insert this row to index 4. Delete index 1 and End the loop.

Hope, this helps....

Vinod

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

Create one more field in the structure which holds the record no.

read the table and then modify the new field with the record no.

Then sort the table based on the record no.

Former Member
0 Kudos

Hi Vinay,

Or you can have one more column in the interanl table which spefies the sequence. While uploading the data you can fill even this field (based on the values you have specified) and in the end sort the internal table with respect to that column.

Former Member
0 Kudos

Hi Vinay,

You may try the steps as follows,

Step 1:

Sort t_ltap_vb  by letyp.

Step 2:

LOOP AT t_ltap_vb into ls_ltap_vb.

           CASE ls_ltap_vb-letyp .

           when 'CTP'.

           APPEND ls_ltap_vb TO t_ltap_vb_temp.

         
          when 'HPL'.

           APPEND ls_ltap_vb TO t_ltap_vb_temp.

          when 'SPL'.

           APPEND ls_ltap_vb TO t_ltap_vb_temp.

          when 'ANC'.

           APPEND ls_ltap_vb TO t_ltap_vb_temp_temp.( DIFFERENT internal table compatible to                                                                                
t_ltap_vb_temp )


           ENDLOOP.
                      

Step 3:

APPEND lines of t_ltap_vb_temp_temp to t_ltap_vb_temp

.

Regards,

Anirban

Former Member
0 Kudos

I know its not the best solution but still.


sort t_ltap_vb by letyp.

loop at t_ltap where letyp eq 'ANC'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  delete t_ltab_vb
endloop.

loop at t_ltap where letyp ne 'CTP' and letyp ne 'HPL' and letyp ne 'SPL' and letyp ne 'ANC'.
  APPEND t_ltap_vb TO t_ltap_vb_temp.
  delete t_ltab_vb
endloop.

append lines of t_ltap_vb_temp to t_ltap_vb.

now ur t_ltap_vb will be sorted accordingly.

Former Member
0 Kudos

Hi Vinay,

Use the below code.

DATA: BEGIN OF itab1 OCCURS 0,

fld(3) TYPE c,

END OF itab1.

DATA: itab2 LIKE itab1 OCCURS 0 WITH HEADER LINE,

itab_final LIKE itab1 OCCURS 0 WITH HEADER LINE.

itab1-fld = 'CTP'.

APPEND itab1.

itab1-fld = 'SPL'.

APPEND itab1.

itab1-fld = 'HPL'.

APPEND itab1.

itab1-fld = 'ANC'.

APPEND itab1.

itab1-fld = 'HPL'.

APPEND itab1.

itab1-fld = 'ANC'.

APPEND itab1.

itab1-fld = 'CTP'.

APPEND itab1.

itab1-fld = 'SPL'.

APPEND itab1.

itab1-fld = 'ANC'.

APPEND itab1.

itab1-fld = 'CTP'.

APPEND itab1.

itab1-fld = 'SPL'.

APPEND itab1.

itab2 [ ] = itab1[ ].

delete itab1 where fld = 'ANC'.

sort itab1 by fld.

delete itab2 where NOT fld = 'ANC'.

sort itab2 by fld.

append lines of itab1 to itab_final.

append lines of itab2 to itab_final.

LOOP AT itab_final.

WRITE:/ itab_final-fld.

ENDLOOP.

Regards,

Kumar Bandanadham

Former Member
0 Kudos

Hi,

You can do like this;

DATA : t_ltap_vb_temp TYPE  ltap_vb OCCURS 0 WITH HEADER LINE .

*Need to sort ltap_vb with letyp value in order of 'CTP' , 'HPL' ,'SPL' , 'ANC'

t_ltap_vb_temp[] = t_ltap_vb[].

DELETE t_ltap_vb WHERE LETYP = 'ANC'.
SORT t_ltap_vb BY LETYP.

LOOP AT t_ltap_vb_temp WHERE LETYP = 'ANC'.
  APPEND t_ltap_vb_temp TO t_ltap_vb. 
ENDLOOP.

This code will help you acheive your result.

Regards

Karthik D

Edited by: Karthik D on May 20, 2009 4:09 PM -

Code Modified by: Karthik D on May 20, 2009 4:15 PM

0 Kudos

The other way to do it without LOOP;

t_ltap_vb_temp[] = t_ltap_vb[].

DELETE t_ltap_vb WHERE Name EQ 'ANC'.
SORT t_ltap_vb BY NAME.

DELETE t_ltap_vb_temp WHERE Name NE 'ANC'.
APPEND LINES OF t_ltap_vb_temp TO t_ltap_vb.

Regards

Karthik D