‎2008 May 20 1:17 PM
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
‎2008 May 20 1:23 PM
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
‎2008 May 20 1:24 PM
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.
‎2008 May 20 1:27 PM
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.
‎2008 May 20 1:53 PM
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
‎2008 May 20 3:20 PM
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
‎2008 May 20 9:41 PM
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..
‎2008 May 21 1:16 AM
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.