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: 

Sorting internal table

Former Member
0 Kudos
214

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
185

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.

9 REPLIES 9

Former Member
0 Kudos
185

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

0 Kudos
185

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.

0 Kudos
185

Then try changing the datatype of the field to integer or any numeric type. This way you can solve your problem.

Former Member
0 Kudos
185

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

Sandeep_Kumar
Product and Topic Expert
Product and Topic Expert
0 Kudos
185

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

Former Member
0 Kudos
186

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.

0 Kudos
185

Hi sreekanth,

Thanks for the reply. The problem is solved.

matt
Active Contributor
0 Kudos
185

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

Former Member
0 Kudos
185

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 .