Skip to Content
author's profile photo Former Member
Former Member

SORTING OF INTERNAL TABLE

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

--

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Sep 06, 2006 at 06: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

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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.

  • Posted on Sep 06, 2006 at 07: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 06, 2006 at 08:45 PM

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

    Rob

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 07, 2006 at 02: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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.