sorting problem

ombhai
ombhai used Ask the Experts™
on
i have aproblem in sorting the customer number in the internal table. i've written "sort itab by kunnr".but still its showing me the output as this
zg001,zg002....zg099 and after this its showing me zg1001and after zg1001 then its printnig zg101.
my question why is it so happening and wats the solution?
 regards
  ombhai
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
u are sorting an alphanumeric field. If u take a look in an ASCII Table, u find out, that a Char "0" stands before a Char "1".
As result of this u find out, that the Row "ZG0000000000" stands before "ZG1"
Dear
  Ombhai
    There is no elegant answer to this. Off course there is a stpid way to do i. Include an index field in the internal table say f. Now let's assume a very simplistic scenario where all kunnr is of the form zgxxxxx where xxxx
is numeric. Now assuming kunnr is of length 10 do this.

f field is of numc(8)
data : tmp_field(8)
loop at itab
 move itab-kunnr+2(8) to tmp_field.
 itab-f = tmp_field
 modify itab
endloop.
sort itab by f.

I know it's a stupid solution

Commented:
You can split the alphabet and numeric portion, then sort it based on the 2 columns.

Below is the working code (tested):

=================================================
report SPLIT_ALPHA_&_NUMBER.

start-of-selection.
data: begin of ITAB occurs 0,
        KUNNR like VBAK-KUNNR,
        ALPHA(10)  TYPE C,
        NUMBER(10) TYPE C,
      end of ITAB.

data: LEN   type I,
      POSITION_OF_NUMBER type I.

* Init table
ITAB-KUNNR = 'ZG1001'.
append ITAB.
ITAB-KUNNR = 'ZG101'.
append ITAB.

PERFORM SPLIT_ALPHA_NUMERIC.
sort ITAB by ALPHA NUMBER.

loop at ITAB.
  write: / ITAB-KUNNR.
endloop.


*-------------------------------------------*
FORM SPLIT_ALPHA_NUMERIC.

loop at ITAB.
* Get the location of first number
  LEN = strlen( ITAB-KUNNR ).
  DO LEN TIMES.
   POSITION_OF_NUMBER = SY-INDEX.
   if ITAB-KUNNR+SY-INDEX(1) ca '0123456789'.
     exit.
   endif.
  ENDDO.

* Split into ALPHA-portion & Number-portion of customer code
  ITAB-ALPHA = ITAB-KUNNR+0(POSITION_OF_NUMBER).
  LEN = LEN - POSITION_OF_NUMBER.
  ITAB-NUMBER = ITAB-KUNNR+POSITION_OF_NUMBER(LEN).
  SHIFT ITAB-NUMBER RIGHT DELETING TRAILING SPACE.
  modify ITAB.

endloop.
ENDFORM.


=================================================

Author

Commented:
thans a lot everybody

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial