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
866

Hi all ,

i have values like this

4421

4413

2121

221

54

66

544

4343

21

2112

66

23

123

123

123

123

when i sort this i am getting the output as

123

123

123

123

21

2112

2121

221

23

4343

4413

4421

54

544

66

66

its sorting only based on the first digit .

Can anyone have an idea about this.

Thanks in advance

5 REPLIES 5
Read only

Laxmana_Appana_
Active Contributor
0 Likes
734

Hi,

what is the field type which stores these values?

1.if it is character field ,the sort works by comparing from the first character onwords .

2.if you are using sort with text option , it works same way like character fields.

3.if you are not mentioned any sort key , then the sort order depends on the sequence of the standard key fields in the internal table. The default key is made up of the non-numeric fields of the table line in the order in which they occur.

Regards

Appana

Read only

0 Likes
734

Hi all,

I am retrieving data from ztable and in z table this field is declared as char type.

Is there any solution for this one.

Thanks in advance

Read only

0 Likes
734

Yes, since the data is stored as character, you need to convert it to a numeric field in your internal table in order to sort correctly. You could also change the data type in your "Z" table to a numeric data type, then the value will already be stored as a numeric, then the sorting will work without conversion. A quick fix for this is to define another field in your internal table a type numeric.

data: begin of itab occurs 0.
      include structure ztable.
data: nfield(10) type n.  " Added numeric field
data: end of itab.

Now you can get the data from your "Z" table into the ITAB. Next loop at the iTAB and move the contents of the field to the numeric field that you have added to the structure and modify the itab.

Loop at itab.
  itab-nfield = itab-cfield.
  modify itab.
endloop.

Now you can sort by this numeric field.

sort itab ascending by nfield.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
734

Use: type i and sort itab by that <column>

REPORT zsam.

data : begin of itab occurs 0,

my_num type i,

end of itab .

itab-my_num = 4421 . append itab .

itab-my_num = 4413 . append itab.

itab-my_num = 2121 . append itab.

itab-my_num = 221 . append itab.

itab-my_num = 54 . append itab.

itab-my_num = 66 . append itab.

itab-my_num = 544 . append itab.

itab-my_num = 4343 . append itab.

itab-my_num = 21 . append itab.

itab-my_num = 2112 . append itab.

itab-my_num = 66 . append itab.

itab-my_num = 23 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

SORT itab by my_num .

loop at itab.

write:/ itab-my_num .

endloop.

Read only

Former Member
0 Likes
734

REPORT zsam.

data : begin of itab occurs 0,

my_num(20) type c,

end of itab .

itab-my_num = 4421 . append itab .

itab-my_num = 4413 . append itab.

itab-my_num = 2121 . append itab.

itab-my_num = 221 . append itab.

itab-my_num = 54 . append itab.

itab-my_num = 66 . append itab.

itab-my_num = 544 . append itab.

itab-my_num = 4343 . append itab.

itab-my_num = 21 . append itab.

itab-my_num = 2112 . append itab.

itab-my_num = 66 . append itab.

itab-my_num = 23 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

itab-my_num = 123 . append itab.

SORT itab by my_num .

loop at itab.

write:/ itab-my_num .

endloop.