2008 Nov 04 6:15 AM
Hi all,
I need to sort an internal table based on a field. The field is of datatype character. The actual data in the field is number. For example consider sample data for the field to be 1, 112, 102, 2. When i try to sort this it is sorted as 1, 102, 112, 2. I need it to be sorted properly as 1, 2, 102, 112. Is it because of the datatype? Is there any solution for it?
Pls help. Thanks in advance.
2008 Nov 04 6:34 AM
REPORT ZSRK_023 .
TYPES : BEGIN OF TP_FINAL,
F1(4) TYPE C,
F2 TYPE C,
F3 TYPE I,
END OF TP_FINAL.
DATA : IT_FINAL TYPE TABLE OF TP_FINAL,
WA_FINAL TYPE TP_FINAL.
WA_FINAL-F1 = '1'.
WA_FINAL-F2 = 'A'.
WA_FINAL-F3 = 1.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '112'.
WA_FINAL-F2 = 'B'.
WA_FINAL-F3 = 2.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '102'.
WA_FINAL-F2 = 'C'.
WA_FINAL-F3 = 3.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '2'.
WA_FINAL-F2 = 'D'.
WA_FINAL-F3 = 4.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
LOOP AT IT_FINAL INTO WA_FINAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = WA_FINAL-F1
IMPORTING
OUTPUT = WA_FINAL-F1.
MODIFY IT_FINAL FROM WA_FINAL.
ENDLOOP.
SORT IT_FINAL BY F1.
LOOP AT IT_FINAL INTO WA_FINAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
INPUT = WA_FINAL-F1
IMPORTING
OUTPUT = WA_FINAL-F1.
WRITE : / WA_FINAL-F1.
ENDLOOP.
2008 Nov 04 6:19 AM
Hi,
Do not post the same questions multiple times. This will not increase answers for you.
Anyways, you can sort internal table by :
SORT <internal_table> BY <fieldname> ASCENDING/DESCENDING.
Regards,
Saba
2008 Nov 04 6:23 AM
I did not post multiple times. It was because nw slowdown.
I am using the same syntax already. By default it is ascending, so this addition is not needed. The problem is with the data type.
2008 Nov 04 6:26 AM
Then try changing the datatype of the field to integer or any numeric type. This way you can solve your problem.
2008 Nov 04 6:22 AM
Hi Ramanan,
Either use the data type as numeric type or u shud add zeros in front before sorting to give you the proper results.
Regards,
Surinder
2008 Nov 04 6:24 AM
Hello Ramanan,
The sorting will work porpely evenb with char data types.
Simple SORT <itab> by <field> will do .
P.S: Please do not create multiple threads for same query!
Rgds,
Sandeep
2008 Nov 04 6:34 AM
REPORT ZSRK_023 .
TYPES : BEGIN OF TP_FINAL,
F1(4) TYPE C,
F2 TYPE C,
F3 TYPE I,
END OF TP_FINAL.
DATA : IT_FINAL TYPE TABLE OF TP_FINAL,
WA_FINAL TYPE TP_FINAL.
WA_FINAL-F1 = '1'.
WA_FINAL-F2 = 'A'.
WA_FINAL-F3 = 1.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '112'.
WA_FINAL-F2 = 'B'.
WA_FINAL-F3 = 2.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '102'.
WA_FINAL-F2 = 'C'.
WA_FINAL-F3 = 3.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
WA_FINAL-F1 = '2'.
WA_FINAL-F2 = 'D'.
WA_FINAL-F3 = 4.
APPEND WA_FINAL TO IT_FINAL.
CLEAR WA_FINAL.
LOOP AT IT_FINAL INTO WA_FINAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = WA_FINAL-F1
IMPORTING
OUTPUT = WA_FINAL-F1.
MODIFY IT_FINAL FROM WA_FINAL.
ENDLOOP.
SORT IT_FINAL BY F1.
LOOP AT IT_FINAL INTO WA_FINAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
INPUT = WA_FINAL-F1
IMPORTING
OUTPUT = WA_FINAL-F1.
WRITE : / WA_FINAL-F1.
ENDLOOP.
2008 Nov 04 6:39 AM
2008 Nov 04 6:41 AM
I've chosen this thread of the duplicates to retain, as it seems to have the most useful answers. In future, if network response results in duplicate posts, for your own benefit, please edit the duplicates with some text like : "Duplicate post, ignore". This will ensure you get responses against only one question.
Thank-you
Matt
2008 Nov 04 7:28 AM
If you change the field type from character to integer or numeric,you will get your objectve.
For the field to be character type you need to proceed as below
REPORT Zsort.
DATA : BEGIN OF ITAB OCCURS 0,
var(3) type c ,
end of itab ,
wa like line of itab .
wa-var = '1'.
append wa to itab .
wa-var = '112'.
append wa to itab .
wa-var = '102'.
append wa to itab .
wa-var = '2'.
append wa to itab .
loop at itab into wa .
write : / wa-var .
endloop .
at line-selection .
loop at itab into wa .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa-var
IMPORTING
OUTPUT = wa-var
.
modify itab from wa.
endloop.
sort itab by var .
break-point .
loop at itab into wa .
write : / wa-var .
endloop .