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 OF INTERNAL TABLE

Former Member
0 Kudos

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

--

8 REPLIES 8

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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

0 Kudos

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

0 Kudos

Technically, NUMC is a character field but comprised of numbers, so the sort would be against the first character.

0 Kudos

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

0 Kudos

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.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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

Former Member
0 Kudos

You might try right-justifying the field, sorting and then left-justifying it.

Rob

Former Member
0 Kudos
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