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 internal table with char components

wolfgang_brunneder
Participant
0 Likes
1,920

Hello!

I have a question concerning sorting internal tables. I have a table with two components of type char:

A B

space space

248 BA

273 space

I tried to sort the table by 'SORT itab BY a b DESCENDING' - the result I expected is ...

A B

273 space

248 BA

space space

... but the computed result is ...

A B

space space

248 BA

273 space

I assume that the computed result is due to the initial char-values (space). How are initial char-values (space) handled by the sort statement?

I appreciate any advice!

Greetings

Wolfgang

1 ACCEPTED SOLUTION
Read only

sarbajitm
Contributor
0 Likes
1,152

To get the expected result..........

what you have to do is...

'SORT itab BY a DESCENDING b DESCENDING'

because the above statement sort itab by a 's descending order first then if two row contain same valu for a then column b help to take the decision which one come first. if row one has smaller value in B than that of row two.then row one come next to row two.

thanks.

6 REPLIES 6
Read only

sarbajitm
Contributor
0 Likes
1,152

hi,

would you please explain your requirement elaborately?

Regards.

Sarbajit.

Read only

0 Likes
1,152

Sorry, I accidently hit the post button before having finished g.

Read only

sarbajitm
Contributor
0 Likes
1,153

To get the expected result..........

what you have to do is...

'SORT itab BY a DESCENDING b DESCENDING'

because the above statement sort itab by a 's descending order first then if two row contain same valu for a then column b help to take the decision which one come first. if row one has smaller value in B than that of row two.then row one come next to row two.

thanks.

Read only

faisalatsap
Active Contributor
0 Likes
1,152

Hi,

Test the Code Bellow it is working as per you requirement

data: BEGIN OF it OCCURS 1,
  a(10),
  b(10),
  END OF it.

  it-a = '273'. it-b = ' '. APPEND it.
  it-a = '248'. it-b = 'BA'. APPEND it.
  it-a = ' '. it-b = ' '. APPEND it.

  SORT it by a DESCENDING b DESCENDING.

Best Regards,

Faisal

Read only

wolfgang_brunneder
Participant
0 Likes
1,152

Thank you two very much - it works!

Greetings

Wolfgang

Read only

Peranandam
Contributor
0 Likes
1,152

Hello,

Dont use coloum having property of charactor.

if you sort internal table based on field having charactor then you will get result as per your computed result.

change your field properties from char to n. then you will get you expected answer.

example if you have internal table with one coloum 'A'.

let us assume value of that coloum is 1,6,12,7,14,7,19

if you sort result will be 1,12,14,6,7,19. so in this case you have to define coloum with numeric property.

Regards,

Peranandam