04-25-2007 6:11 PM
Hello,
Is it possible to sort a table by field position? For example, I have table ITAB with a 4 character field F1. I want to sort the table first by the 3rd position ( itab-f12(1) ) ascending then the 4th position ( itab-f13(1) ) descending.
data: begin of itab occurs 0,
f1(4),
end of itab.
itab-f1 = '01AC'.
append itab.
itab-f1 = '02AB'.
append itab.
itab-f1 = '01CD'.
append itab.
itab-f1 = '02CA'.
append itab.
sort itab...
I thought about using TRANSLATE with a rule and converting each position to a numeric value (then transfer the value to a new 'sort' field), but I wasn't sure if there was an easier way.
The sorted table should read: '01AC' ; '02AB' ; '01CD' ; '02CA'
Any help is appreciated.
04-25-2007 6:28 PM
Hi,
Please try this.
DATA: BEGIN OF ITAB OCCURS 0,
F1(4),
END OF ITAB.
DATA: BEGIN OF ITAB2 OCCURS 0,
F1(1),
F2(1),
F3(4),
END OF ITAB2.
ITAB-F1 = '01AC'.
APPEND ITAB.
ITAB-F1 = '02AB'.
APPEND ITAB.
ITAB-F1 = '01CD'.
APPEND ITAB.
ITAB-F1 = '02CA'.
APPEND ITAB.
LOOP AT ITAB.
ITAB2-F1 = ITAB-F1+2(1).
ITAB2-F2 = ITAB-F1+3(1).
ITAB2-F3 = ITAB-F1.
APPEND ITAB2.
ENDLOOP.
SORT ITAB2 BY F1 ASCENDING
F2 DESCENDING.
LOOP AT ITAB2.
WRITE: / ITAB2-F3.
ENDLOOP.
Regards,
Ferry Lianto
04-25-2007 6:28 PM
Hi,
Please try this.
DATA: BEGIN OF ITAB OCCURS 0,
F1(4),
END OF ITAB.
DATA: BEGIN OF ITAB2 OCCURS 0,
F1(1),
F2(1),
F3(4),
END OF ITAB2.
ITAB-F1 = '01AC'.
APPEND ITAB.
ITAB-F1 = '02AB'.
APPEND ITAB.
ITAB-F1 = '01CD'.
APPEND ITAB.
ITAB-F1 = '02CA'.
APPEND ITAB.
LOOP AT ITAB.
ITAB2-F1 = ITAB-F1+2(1).
ITAB2-F2 = ITAB-F1+3(1).
ITAB2-F3 = ITAB-F1.
APPEND ITAB2.
ENDLOOP.
SORT ITAB2 BY F1 ASCENDING
F2 DESCENDING.
LOOP AT ITAB2.
WRITE: / ITAB2-F3.
ENDLOOP.
Regards,
Ferry Lianto
04-25-2007 8:28 PM
I thought of that solution, but I was trying for something that doesn't require a 'work table' or translating based on a rule. It may not be possible.
04-25-2007 8:31 PM
Hi Cal,
I don't think possible without creating another internal table.
Regards,
Ferry Lianto
04-25-2007 9:41 PM
Hi,
You cannot sort using some characters of the field. You will need internal table to sort it.