Application Development and Automation 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: 
Read only

sorting rule

Former Member
0 Likes
715

Hi All,

i have to sort a set of records by one field.. this field could be blank and may have digits as well..

So i need to sort this internal table such that blank spaces have a precedence over alphabets and alphabets over digits.

How do i go about doing this..

Thanks in advance,

Herwin.

6 REPLIES 6
Read only

Former Member
0 Likes
662

Hi,

Simply sort with your field ascending. Then the space will be the firs line.

Precedence is like SPACE then Numeric then Special Character then Alphabets

Read only

0 Likes
662

Ya the space would be first, but i want alpha before digits.

Read only

0 Likes
662

HI check this code it is working as u need.

TYPES: begin of t_c,

c type char1,

end of t_c.

data: t_tab type STANDARD TABLE OF t_c.

data w_c type t_c.

data:t_tab1 type STANDARD TABLE OF t_c.

w_c-c = space.

append w_c to t_tab.

w_c-c = '1'.

append w_c to t_tab.

w_c-c = 'a'.

append w_c to t_tab.

sort t_tab DESCENDING.

loop at t_tab into w_c where c is INITIAL.

append w_c to t_tab1.

endloop.

loop at t_tab into w_c where not c is initial .

append w_c to t_tab1.

endloop.

if sy-subrc = 0.

endif.

Check the entries in tab t_tab1 in debug. It is according to your requirement I hope.

Edited by: Anurag_n on Feb 23, 2010 12:03 PM

Read only

0 Likes
662

But this doesnt solve my problem its in descending order which shouldn't be the case.

Read only

0 Likes
662

Hi,

have you seen the entries in table t_itab1. If you check in debugger.. First line is blank second contain 'a' and third contain '1'

So i need to sort this internal table such that blank spaces have a precedence over alphabets and alphabets over digits.

Read only

0 Likes
662

Hello

Try this logic:


data: begin of itab occurs 0,
      x(1),
      y(1),
      end of itab.
itab-y = '1'. append itab.
itab-y = ' '. append itab.
itab-y = 'A'. append itab.
itab-y = ' '. append itab.
itab-y = ' '. append itab.
itab-y = '3'. append itab.
itab-y = 'B'. append itab.
itab-y = ' '. append itab.
itab-y = '2'. append itab.
itab-y = 'C'. append itab.
itab-y = '8'. append itab.
itab-y = '1'. append itab.
itab-y = ' '. append itab.
loop at itab.
  if itab-y eq space.
    itab-x = '1'. modify itab.
  else.
    if itab-y CO '0123456789'.
      itab-x = '3'. modify itab.
    else.
      itab-x = '2'. modify itab.
    endif.
  endif.
endloop.

sort itab.