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 Issue

Former Member
0 Likes
611

Hi Experts,

I have one query in sorting. I have one column as Character type.

In that column it contains values like 6-9, 10-15, 16, 17, 18 kind of.

Now while sorting I wanted that it should display like

6-9

10-15

16

17

18.

If I do using sort statement it is not coming in desired format. Can anybody will suggest me how I can do that?

Regards,

Neha

6 REPLIES 6
Read only

rvinod1982
Contributor
0 Likes
577

Hi Neha,

Is it possible to change the value of 6-9 to 06-09?

If you change it then the sorting will be proper.

Regards,

Vinod

Read only

Former Member
0 Likes
577

Hi,

i think you can acheive this only by manipulating data when you are creating the internal table

check the last entered value for that column , check if the next value is greater - something like that

Regards

Sajimon Chandran

Read only

Former Member
0 Likes
577

Hi,

Try this way....

data: begin of it,
        chk type i,
        fld type char6,
      end of it.
data: itab like table of it with header line.

MOVE '1' TO itab-chk.
MOVE '6-9' TO itab-fld.
APPEND itab.

MOVE '2' TO itab-chk.
MOVE '10-15' TO itab-fld.
APPEND itab.

MOVE '3' TO itab-chk.
MOVE '16-20' TO itab-fld.
APPEND itab.

MOVE '4' TO itab-chk.
MOVE '21' TO itab-fld.
APPEND itab.

MOVE '5' TO itab-chk.
MOVE '22' TO itab-fld.
APPEND itab.
MOVE '23' TO itab-fld.
APPEND itab.

SORT ITAB ASCENDING BY chk.
LOOP AT ITAB.
WRITE:/ ITAB-FLD.
ENDLOOP.

Regards,

Shankar.

Read only

Former Member
0 Likes
577

Hi Neha,

You can use a temporary supplementary field in your internal table, say num, that you can fill automatically with the first number and sort by this column :


  TYPES : BEGIN OF ts_test,
            col TYPE string,  " column with your original field
            num TYPE i,       " column with the first number (integer for the sort)
          END OF ts_test.

  DATA : lt_test TYPE tt_test,
         ld_test TYPE i.

  LOOP AT lt_test ASSIGNING <ls_test>.
" Split your field at symbol "-"
    FIND '-' IN <ls_test>-col MATCH OFFSET ld_test.
    IF sy-subrc = 0.
" If found keep only first number
      MOVE <ls_test>-col(ld_test) TO <ls_test>-num.
    ELSE.
" else keep your field
      MOVE <ls_test>-col TO <ls_test>-num.
    ENDIF.
  ENDLOOP.

  SORT lt_test BY num.

Hope it help!

Samuel

Read only

Former Member
0 Likes
577

Hi,

Make 1 internal table with 2 fields.

split your target characters on the basis of '-'. And Append into the internal table field respectively.Then sort the table on the basis of first field then second field. Then concatenate the fields of internal table with '-' , if the second field is not vacant.

Read only

Former Member
0 Likes
577

d