‎2014 Aug 04 7:33 AM
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.
‎2014 Aug 04 8:49 AM
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.
‎2014 Aug 04 7:38 AM
‎2014 Aug 04 7:50 AM
Hi Philip,
No....only I have field1 and field2 in the sort statement.
‎2014 Aug 04 7:49 AM
Hi Kavi,
Try this, SORT itab1 BY field1 field2 STABLE.
regards,
Archer
‎2014 Aug 04 7:53 AM
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.
‎2014 Aug 04 8:00 AM
But you need Field 3 to be sorted descending right? Then why are you not adding Field3 into the sort criteria?
Regards,
Philip.
‎2014 Aug 04 8:24 AM
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.
‎2014 Aug 04 8:00 AM
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
‎2014 Aug 04 9:45 AM
Thanks Vidhyasagar...it is very helpful for me to findout the reason..thanks much...
‎2014 Aug 04 8:06 AM
‎2014 Aug 04 8:24 AM
‎2014 Aug 04 8:47 AM
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
‎2014 Aug 04 8:49 AM
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.