‎2009 May 11 11:51 AM
Hi all,
I was trying to SORT an internal table in ASCENDING order based on
a Char type field. What's happening is, it is being sorted only based on the comparision of 1'st character in every record.
Below is the output i get.
1
123
21
310
7
Expected output
1
7
21
123
310
Please correct my code / give some suggestion to achieve this.
DATA : BEGIN OF it_tab1 OCCURS 0,
f1 type char10,
end of it_tab1.
it_tab1-f1 = '1'.
append it_tab1.
it_tab1-f1 = '7'.
append it_tab1.
it_tab1-f1 = '21'.
append it_tab1.
it_tab1-f1 = '310'.
append it_tab1.
it_tab1-f1 = '123'.
append it_tab1.
SORT it_tab1 ASCENDING BY f1.
Loop at it_tab1.
write : / it_tab1-f1.
endloop.
‎2009 May 11 11:57 AM
‎2009 May 11 11:54 AM
as the field type is CHAR type,the Sort is happeneing like given..
why cant change the field type..is it that u want only to be char type..go for integer type
Edited by: Sravan Kumar on May 11, 2009 4:24 PM
‎2009 May 11 11:57 AM
Hi Sravan,
Yes, i want that field to be of CHAR type.....
The example which i'd shown is a sample one to explain the problem. My real program has to sort the table based on a CHAR type field.
‎2009 May 11 11:55 AM
Just an idea: add a numeric field to the internal table, copy the content of the character field to the numeric field and then sort the internal table on the numeric field. Not sure if it works though...
‎2009 May 11 11:57 AM
‎2009 May 11 11:59 AM
Hi John,
Declare f1 as type i(Integer variable.)
Regards,
Lakshman.
‎2009 May 11 12:01 PM
Any suggestions dealing with CHAR type ...rather than changing to Integer or numeric...as in my original prog...i HAVE to use the sorting field as only CHAR type.
‎2009 May 11 12:00 PM
Hi,
Use the following code
DATA : BEGIN OF it_tab1 OCCURS 0,
f1 type i,
end of it_tab1.
it_tab1-f1 = '1'.
append it_tab1.
it_tab1-f1 = '7'.
append it_tab1.
it_tab1-f1 = '21'.
append it_tab1.
it_tab1-f1 = '310'.
append it_tab1.
it_tab1-f1 = '123'.
append it_tab1.
SORT it_tab1 ASCENDING BY f1.
Loop at it_tab1.
write : / it_tab1-f1.
endloop.
‎2009 May 11 12:00 PM
HI,
Create an another field of numeric type for the same data in ur internal table.
And sort that table table with the numeric field.
‎2009 May 11 12:01 PM
Hi John,
Ya as Rolf said try moving the value to some variable of type 'i'.
Like this,
DATA : BEGIN OF it_tab1 OCCURS 0,
f1 type char10,
end of it_tab1,
it_num type i.
it_tab1-f1 = '7'.
move it_tab-f1 to it_num.
append it_num to it_tab1.
SORT it_tab1 ASCENDING BY it_num.
Loop at it_tab1.
write : / it_num.
endloop.Try using this logic.
Much Regards,
Amuktha.
‎2009 May 11 12:01 PM
Hi,
Unless u change the data type from Char to INT/NUMC u cannot get the desired output.
u write a logic such that after filling ur intenal table( which contains CHAR) , move the data to another internal table ( containing INT/NUMC).
Regards,
Naveen
‎2009 May 11 12:06 PM
Hi John,
Try using CONVERT_EXIT_ALPHA_INPUT .It adds the leading zeroes infront.
Regards,
Lakshman.
‎2009 May 11 1:46 PM
Hi ,
Please give the value to the field without quotes. it takes the value and sort it. I checked it. It is working fine. Kindly try.
DATA : BEGIN OF it_tab1 OCCURS 0,
f1 type char10,
end of it_tab1.
it_tab1-f1 = 1.
append it_tab1.
it_tab1-f1 = 7.
append it_tab1.
it_tab1-f1 = 21.
append it_tab1.
it_tab1-f1 = 310.
append it_tab1.
it_tab1-f1 = 123.
append it_tab1.
SORT it_tab1 by f1.
Loop at it_tab1.
write : / it_tab1-f1.
endloop.
Thanks,
Uma
‎2009 May 11 2:19 PM
‎2009 May 11 2:28 PM
Pass values as number , not as character . see the following i have removed single codes while populating data to work area.
Now try it. it will work .
Ex: it_tab1-f1 = 1.
append it_tab1.
it_tab1-f1 = 7.
append it_tab1.
it_tab1-f1 = 21.
append it_tab1.
it_tab1-f1 = 310.
append it_tab1.
it_tab1-f1 = 123.
append it_tab1.