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

Former Member
0 Likes
671

I have internal table and need to sort it based on following

for recordds where field 1 = A

I need to sort by field 3

for records where field 1 = C

I need to sort by field 4

7 REPLIES 7
Read only

gabriel_braun
Explorer
0 Likes
653

I suggest to add an additional column, that contains the value of column 3 for the A records and the value of column 4 for the C records. Then you can sort by this additional column and you have your result.

Regards

Gabriel

Read only

Former Member
0 Likes
653

You'll need to build a sort key area in the internal table to do this.

Create a common field that contains both field 3 and 4 then sort on field 1 and the built sort key.

Read only

Former Member
0 Likes
653

not possible.

all you can do is to seperate your records and have to ones with field 1 = C in one itab and the others in another itab.

OR

before the sort you could move the value of field 4 to field 3 if field 1 = C.

now you have field 3 as a sort criteria.

Read only

Former Member
0 Likes
653

hi this is also one sol i think..

sort itab by field1 ..

loop at itab where field1 = 'A'

and field1 = 'C'.

endloop.

for more data check this..

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/sort.htm

regards,

venkat

Read only

former_member435013
Active Participant
0 Likes
653

Hi,

perhaps you may do it like this. The original table is named tab. The sorted table is built up step by step and name ntab. At the end tab is empty and ntab is sorted

- 1st part with field1 = 'A'

- 2nd part with field1 = 'C'

- 3rd part with field1 neither nor

REPORT test.

TYPES:

BEGIN OF ele_t,

field1,

field2,

field3,

field4,

END OF ele_t.

DATA:

tab TYPE ele_t OCCURS 0 WITH HEADER LINE,

ntab TYPE ele_t OCCURS 0 WITH HEADER LINE,

ind TYPE i.

tab-field1 = 'A'.

tab-field2 = 'U'.

tab-field3 = '1'.

tab-field4 = '6'.

APPEND tab.

tab-field1 = 'B'.

tab-field2 = 'U'.

tab-field3 = '2'.

tab-field4 = '5'.

APPEND tab.

tab-field1 = 'C'.

tab-field2 = 'W'.

tab-field3 = '3'.

tab-field4 = '4'.

APPEND tab.

tab-field1 = 'A'.

tab-field2 = 'X'.

tab-field3 = '4'.

tab-field4 = '3'.

APPEND tab.

tab-field1 = 'B'.

tab-field2 = 'Y'.

tab-field3 = '5'.

tab-field4 = '2'.

APPEND tab.

tab-field1 = 'C'.

tab-field2 = 'Z'.

tab-field3 = '6'.

tab-field4 = '1'.

APPEND tab.

SORT tab BY field3.

ind = 1.

DO.

READ TABLE tab INDEX ind.

IF NOT sy-subrc IS INITIAL.

EXIT.

ENDIF.

IF tab-field1 = 'A'.

APPEND tab TO ntab.

DELETE tab INDEX ind.

ELSE.

ind = ind + 1.

ENDIF.

ENDDO.

SORT tab BY field4.

ind = 1.

DO.

READ TABLE tab INDEX ind.

IF NOT sy-subrc IS INITIAL.

EXIT.

ENDIF.

IF tab-field1 = 'C'.

APPEND tab TO ntab.

DELETE tab INDEX ind.

ELSE.

ind = ind + 1.

ENDIF.

ENDDO.

ind = 1.

DO.

READ TABLE tab INDEX ind.

IF NOT sy-subrc IS INITIAL.

EXIT.

ENDIF.

APPEND tab TO ntab.

DELETE tab INDEX ind.

ENDDO.

BREAK-POINT.

regards

Walter Habich

Read only

Former Member
0 Likes
653

Hi..

This can be one solution..

Define 2 tables with same structure as your internal table say tab_a and tab_c.


loop at itab.
case field1.
when 'A'.
move to tab_a.
when 'C'.
move to tab_c.
endloop.

sort tab_a by field 3.
sort tab_c by field 4.

refresh itab.
itab[] = tab_a[]
APPEND LINES OF tab_c TO itab.

I think this will work..

reward if helpful..

Ags..

Read only

0 Likes
653

If you don't want to use 3 internal tables

TYPES : BEGIN OF ty_data,

field1(10), field2(10),

field3(10), field4(10),

END OF ty_data.

DATA : it_data TYPE TABLE OF ty_data WITH HEADER LINE.

DATA : it_data1 TYPE TABLE OF ty_data WITH HEADER LINE.

DEFINE fill_table.

&1-field1 = &2. &1-field2 = &3.

&1-field3 = &4. &1-field4 = &5.

append &1.

END-OF-DEFINITION.

fill_table it_data 'A' '1' 'U' '6'.

fill_table it_data 'B' 'U' '2' '5'.

fill_table it_data 'C' 'W' '3' '4'.

fill_table it_data 'A' 'X' '4' '3'.

fill_table it_data 'B' 'Y' '5' '2'.

fill_table it_data 'C' 'Z' '6' '1'.

APPEND LINES OF it_data TO it_data1.

SORT it_data1 BY field1 field3.

DELETE it_data1 WHERE field1 NE 'A'.

DELETE it_data WHERE field1 = 'A'.

SORT it_data BY field1 field4.

APPEND LINES OF it_data TO it_data1.

LOOP AT it_data1.

WRITE : / it_data1-field1, it_data1-field2,

it_data1-field3, it_data1-field4.

ENDLOOP.