09-06-2006 7:50 PM
Hi all,
I WANT TO KNOW WHETHER SORTING AN INTERNAL TABLE BASED ON FIELD TYPE NUMERIC GIVES THE SAME RESULT AS SORTING DOES ON CHAR FIELD.
EX
1
10
2
21
3
I.E BASED ON FIRST DIGIT.
SO IS THERE ANY SOLUTION SO THAT I CAN SORT MY INTERNAL LIKE
1
2
3
10
11
--
09-06-2006 7:57 PM
THey will be different results, because 1 and 10 can be right next to each other when dealing with character fields, it is best to fill with leading zeros or use a numeric.
For example, even though this is a character the sorting still works because of the leading zeros.
report zrich_0001.
data: begin of itab occurs 0,
char(5) type c,
end of itab.
itab-char = '00004'. append itab.
itab-char = '00003'. append itab.
itab-char = '00005'. append itab.
itab-char = '00009'. append itab.
itab-char = '00008'. append itab.
itab-char = '00010'. append itab.
itab-char = '00001'. append itab.
itab-char = '00007'. append itab.
itab-char = '00006'. append itab.
itab-char = '00002'. append itab.
sort itab ascending by char.
loop at itab.
write:/ itab-char.
endloop.
Regards,
Rich Heilman
09-06-2006 9:02 PM
Hi Rich,
sorting with numeric field is similar to char field.
I am sorting itab based in field whose domain is type numc
even you sort on numeric it sorts based on first digit
That is what iam seeing here
Thanks
let me if iam wrong
09-06-2006 9:04 PM
Technically, NUMC is a character field but comprised of numbers, so the sort would be against the first character.
09-06-2006 9:05 PM
If the field is truely numeric, this means that the data is stored with leading zeros. Which means that the sorting will work correctly when sorting numbers. Do you have sample code that illistrates your problem?
Are you sure that your internal table field is defined as a numeric?
Regards,
RIch Heilman
09-07-2006 3:58 AM
Hi Swathi,
I tried this.
Data: begin of itab occurs 0,
field(2) type n,
end of itab.
itab-field = 1.
append itab.
itab-field = 10.
append itab.
itab-field = 2.
append itab.
itab-field = 20.
append itab.
Sort itab by field.
loop at itab.
write: itab-field.
endloop.
And the ans is
01
02
10
20.
I dont see why you should get a problem in sorting numeric types.
Please write a piece of ur code here.
09-06-2006 8:02 PM
If you do not want to disturb the character field, add another field to your internal table that is type numeric, when filling the character field with data, also fill this numeric field, then sort by the numeric field instead.
report zrich_0001.
data: begin of itab occurs 0,
char(5) type c,
numc(5) type n,
end of itab.
itab-char = '4'. itab-numc = itab-char. append itab.
itab-char = '3'. itab-numc = itab-char. append itab.
itab-char = '5'. itab-numc = itab-char. append itab.
itab-char = '9'. itab-numc = itab-char. append itab.
itab-char = '8'. itab-numc = itab-char. append itab.
itab-char = '10'.itab-numc = itab-char. append itab.
itab-char = '1'. itab-numc = itab-char. append itab.
itab-char = '7'. itab-numc = itab-char. append itab.
itab-char = '6'. itab-numc = itab-char. append itab.
itab-char = '2'. itab-numc = itab-char. append itab.
sort itab ascending by numc.
loop at itab.
write:/ itab-char.
endloop.
Regards,
RIch Heilman
09-06-2006 9:45 PM
You might try right-justifying the field, sorting and then left-justifying it.
Rob
09-07-2006 3:35 AM
Hi Swathi,
chk out this
after sorting again u can remove leading zeroes
REPORT YCHATEST .
data : begin of itab occurs 0,
houseno(10),
end of itab.
itab-houseno = '24'.
append itab.
clear itab.
itab-houseno = '4'.
append itab.
clear itab.
itab-houseno = '454'.
append itab.
clear itab.
loop at itab.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = itab-houseno
IMPORTING
OUTPUT = itab-houseno
.
modify itab index sy-tabix.
endloop.
sort itab by houseno.
loop at itab.
write : / itab-houseno.
endloop.
loop at itab.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = itab-houseno
IMPORTING
OUTPUT = itab-houseno
.
modify itab index sy-tabix.
endloop.
loop at itab.
write : / itab-houseno.
endloop.
Message was edited by: Chandrasekhar Jagarlamudi