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

About sorting in the SORT instruction...

Former Member
0 Likes
1,640

Hello,

I have searched if someone posted this question before, but I couldn't find it:

How does the SORT instruction actually sort the table that is passed? Which is the sorting algorithm used?

And: Why is it an instruction?

Just curious...

Thanks a lot!,

Eloi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,252

The documentation does not mention it but as far as I know the SORT command is implemented using quick sort. This also explains why the sort is not stable.

Since ABAP is a 4th generation language, it would be pretty weird to not have a SORT command. And an implementation in the kernel will always be faster than anything built in ABAP. Why you need it in your program is a whole different story, I see it misused a lot where the sorting could have been done during SELECT or the sort is only done to remove duplicates that could have been avoided with some smarter SELECTs.

5 REPLIES 5
Read only

Former Member
0 Likes
1,252

hi,

for sorting we follow the linear search,binary search and hash based search algorithms. These algorithms are depend on type of internal table.

Read only

Former Member
0 Likes
1,253

The documentation does not mention it but as far as I know the SORT command is implemented using quick sort. This also explains why the sort is not stable.

Since ABAP is a 4th generation language, it would be pretty weird to not have a SORT command. And an implementation in the kernel will always be faster than anything built in ABAP. Why you need it in your program is a whole different story, I see it misused a lot where the sorting could have been done during SELECT or the sort is only done to remove duplicates that could have been avoided with some smarter SELECTs.

Read only

0 Likes
1,252

SORT command allows to use [STABLE] clause therefore it has to change, in that case, its sorting method.

It's interesting what you said about sorting misuse. So far I have read through the forums to be a good practice to assign the effort of sorting to the application server rather than the database one. No?

Thanks for your reply Gerrit.

Read only

Former Member
0 Likes
1,252

As per me Sorting algorithm varies based on system to system and different parameters.

E.G :

1. Application server type : Linux/Unix/Windows/Sybase/Solaris.

2. System type : 32 bit/64 bit

3. Unicode system : Yes/No

4. Type of internal table : Hashed/Sorted/Standard

Check this note 193529 also

193529 - Sort as of 4.5A/EXSORT_NOT_ENOUGH_MEMORY

They use a term called "Disk sort" which is more closer to Merge Sort we have in C, when the data becomes higher than a certain limit and in some cases they use quick sort.

Also I agree in a typical 3 tier environment it is recommended to do the sorting in application server rather in database level but is about to change with SAP HANA.

R

Read only

Former Member
0 Likes
1,252

This message was moderated.