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 the Characters

Former Member
0 Likes
2,889

Hello friends,

I have got an issue while sorting the Character values. Not sure why the same sort statement working differently with the same code in different systems (Dev and quality servers).Example of the issue is described below:

In quality:

We have internal table itab1 with the below format:

field1     field2     field3

1001     101        ab1 

1001     101        ab2

1001     101        ab3

1001     101        ab4

1001     101        ab5

The above internal table, after sorting with field1 and field2 , it sorted as below: (SORT itab1 BY field1 field2)

field1     field2     field3

1001     101        ab5 

1001     101        ab4

1001     101        ab3

1001     101        ab2

1001     101        ab1

Whereas, in dev server, it is not getting sorted, since the values inbotht the fields are same. And this is how it should work in quality server.

Could you please helpme in finding out the reason? Thanks in advance.

1 ACCEPTED SOLUTION
Read only

former_member212124
Active Participant
0 Likes
2,177

Hi,

The link I provided will explain the reason with example.

Please refer that.

SORT keyword sorts the internal table by the field we mention in the statement, either it can be ascending or descending. But the sort sequence will not be preserved, i.e, if we have a field with similar values for more than once the sort sequence might differ everytime we sort the table on that particular field. To preserve the sequence we can use keyword SORT STABLE.

SORT STABLE keyword sorts the internal table and preserves the sort sequence. When we try to re-sort the table on the same sort criteria the sequence will not changed.

12 REPLIES 12
Read only

philipdavy
Contributor
0 Likes
2,177
  • Did you add Sort Field3 Descending.  ?

Regards,

Philip.

Read only

0 Likes
2,177

Hi Philip,

No....only I have field1 and field2 in the sort statement.

Read only

Former Member
0 Likes
2,177

Hi Kavi,

Try this, SORT itab1 BY field1 field2 STABLE.

regards,

Archer

Read only

0 Likes
2,177

Hi Dengyong,

Thanks for the suggestion. But this issue is not the code changes, I have to analyse and need to give them the reason on why the same statement is working like that in Quality server.

Read only

0 Likes
2,177

But you need Field 3 to be sorted descending right? Then why are you not adding Field3 into the sort criteria?

Regards,

Philip.

Read only

0 Likes
2,177

Hi Philip,

I do not want to sort field3 as decscneding.

My question is :

I want to know the reason why it is sorting in descending order.

Read only

former_member212124
Active Participant
0 Likes
2,177

Hi Kavi,

Do as Dengyong said use SORT with STABLE.

this link will help you.

Difference Between SORT and SORT STABLE - Code Gallery - SCN Wiki

IF I did not know STABLE, I would use SORT itab1  by field1 field2 field3.

thanks,

vidyasagar

Read only

0 Likes
2,177

Thanks Vidhyasagar...it is very helpful for me to findout the reason..thanks much...

Read only

former_member202818
Active Contributor
0 Likes
2,177

Check data types of fields in both system.

Read only

0 Likes
2,177

Data types are same in both the systems.

Read only

Former Member
0 Likes
2,177

Hi Kavi,

I think you are looking for the root cause to make an explanation to someone who is asking you and I don't see an answer get the point.

Guess it's related to the sorting algorithm(quick sort http://en.wikipedia.org/wiki/Quicksort) which is used by SAP.

For the sorting algorithm , the quick sort will be executed based on a pivot which will be randomly picked and be affected by the content of the internal table.

Probably that's the reason why you can see different results in different systems.

Thanks,

Sam

Read only

former_member212124
Active Participant
0 Likes
2,178

Hi,

The link I provided will explain the reason with example.

Please refer that.

SORT keyword sorts the internal table by the field we mention in the statement, either it can be ascending or descending. But the sort sequence will not be preserved, i.e, if we have a field with similar values for more than once the sort sequence might differ everytime we sort the table on that particular field. To preserve the sequence we can use keyword SORT STABLE.

SORT STABLE keyword sorts the internal table and preserves the sort sequence. When we try to re-sort the table on the same sort criteria the sequence will not changed.