Link to home
Start Free TrialLog in
Avatar of ombhai
ombhai

asked on

sorting problem

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
Avatar of okionka
okionka

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"
ASKER CERTIFIED SOLUTION
Avatar of quest_inc
quest_inc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.


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

Avatar of ombhai

ASKER

thans a lot everybody