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

SORT an Internal table !

Former Member
0 Likes
1,481

Hi all,

I was trying to SORT an internal table in ASCENDING order based on

a Char type field. What's happening is, it is being sorted only based on the comparision of 1'st character in every record.

Below is the output i get.

1

123

21

310

7

Expected output

1

7

21

123

310

Please correct my code / give some suggestion to achieve this.

DATA : BEGIN OF it_tab1 OCCURS 0,

f1 type char10,

end of it_tab1.

it_tab1-f1 = '1'.

append it_tab1.

it_tab1-f1 = '7'.

append it_tab1.

it_tab1-f1 = '21'.

append it_tab1.

it_tab1-f1 = '310'.

append it_tab1.

it_tab1-f1 = '123'.

append it_tab1.

SORT it_tab1 ASCENDING BY f1.

Loop at it_tab1.

write : / it_tab1-f1.

endloop.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,450

Hi..

Try defining f1 as TYPE NUMC10...

14 REPLIES 14
Read only

former_member242255
Active Contributor
0 Likes
1,450

as the field type is CHAR type,the Sort is happeneing like given..

why cant change the field type..is it that u want only to be char type..go for integer type

Edited by: Sravan Kumar on May 11, 2009 4:24 PM

Read only

0 Likes
1,450

Hi Sravan,

Yes, i want that field to be of CHAR type.....

The example which i'd shown is a sample one to explain the problem. My real program has to sort the table based on a CHAR type field.

Read only

Former Member
0 Likes
1,450

Just an idea: add a numeric field to the internal table, copy the content of the character field to the numeric field and then sort the internal table on the numeric field. Not sure if it works though...

Read only

Former Member
0 Likes
1,451

Hi..

Try defining f1 as TYPE NUMC10...

Read only

former_member209217
Active Contributor
0 Likes
1,450

Hi John,

Declare f1 as type i(Integer variable.)

Regards,

Lakshman.

Read only

0 Likes
1,450

Any suggestions dealing with CHAR type ...rather than changing to Integer or numeric...as in my original prog...i HAVE to use the sorting field as only CHAR type.

Read only

Former Member
0 Likes
1,450

Hi,

Use the following code

DATA : BEGIN OF it_tab1 OCCURS 0,

f1 type i,

end of it_tab1.

it_tab1-f1 = '1'.

append it_tab1.

it_tab1-f1 = '7'.

append it_tab1.

it_tab1-f1 = '21'.

append it_tab1.

it_tab1-f1 = '310'.

append it_tab1.

it_tab1-f1 = '123'.

append it_tab1.

SORT it_tab1 ASCENDING BY f1.

Loop at it_tab1.

write : / it_tab1-f1.

endloop.

Read only

Former Member
0 Likes
1,450

HI,

Create an another field of numeric type for the same data in ur internal table.

And sort that table table with the numeric field.

Read only

Former Member
0 Likes
1,450

Hi John,

Ya as Rolf said try moving the value to some variable of type 'i'.

Like this,

DATA : BEGIN OF it_tab1 OCCURS 0,
f1 type char10,
end of it_tab1,
it_num type i.


it_tab1-f1 = '7'.
move it_tab-f1 to it_num.
append it_num to it_tab1.


SORT it_tab1 ASCENDING BY it_num.

Loop at it_tab1.
write : / it_num.
endloop.

Try using this logic.

Much Regards,

Amuktha.

Read only

Former Member
0 Likes
1,450

Hi,

Unless u change the data type from Char to INT/NUMC u cannot get the desired output.

u write a logic such that after filling ur intenal table( which contains CHAR) , move the data to another internal table ( containing INT/NUMC).

Regards,

Naveen

Read only

0 Likes
1,450

Hi John,

Try using CONVERT_EXIT_ALPHA_INPUT .It adds the leading zeroes infront.

Regards,

Lakshman.

Read only

UmaArjunan
Active Participant
0 Likes
1,450

Hi ,

Please give the value to the field without quotes. it takes the value and sort it. I checked it. It is working fine. Kindly try.

DATA : BEGIN OF it_tab1 OCCURS 0,

f1 type char10,

end of it_tab1.

it_tab1-f1 = 1.

append it_tab1.

it_tab1-f1 = 7.

append it_tab1.

it_tab1-f1 = 21.

append it_tab1.

it_tab1-f1 = 310.

append it_tab1.

it_tab1-f1 = 123.

append it_tab1.

SORT it_tab1 by f1.

Loop at it_tab1.

write : / it_tab1-f1.

endloop.

Thanks,

Uma

Read only

0 Likes
1,450

yes its working

Read only

Former Member
0 Likes
1,450

Pass values as number , not as character . see the following i have removed single codes while populating data to work area.

Now try it. it will work .

Ex: it_tab1-f1 = 1.

append it_tab1.

it_tab1-f1 = 7.

append it_tab1.

it_tab1-f1 = 21.

append it_tab1.

it_tab1-f1 = 310.

append it_tab1.

it_tab1-f1 = 123.

append it_tab1.