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

Inconsistency when sorting internal table

Former Member
0 Likes
2,529

My Internal table has values as

KNUMH KOPOS KLFN1 KSTBW

0000$1   01         0001      0.000

0000$1   01         0002      12.000

0000$1   01         0003      15.000

0000$1   01         0004      17.000

0000$1   01         0005      19.000

0000$1   01         0006      21.000

0000$2   02         0001      0.000

0000$2   02         0002      12.000

0000$2   02         0003      15.000

0000$2   02         0004      17.000

0000$2   02         0005      19.000

0000$2   02         0006      21.000

0000$3   03         0001      0.000

0000$3   03         0002      12.000

0000$3   03         0003      15.000

0000$3   03         0004      17.000

0000$3   03         0005      19.000

0000$3   03         0006      21.000

0000$4   04         0001      0.000

0000$4   04         0002      12.000

0000$4   04         0003      15.000

0000$4   04         0004      17.000

0000$4   04         0005      19.000

0000$4   04         0006      21.000

0000$5   05         0001      0.000

0000$5   05         0002      12.000

0000$5   05         0003      15.000

0000$5   05         0004      17.000

0000$5   05         0005      19.000

0000$5   05         0006      21.000

0000$6   06         0001      0.000

0000$6   06         0002      12.000

0000$6   06         0003      15.000

0000$6   06         0004      17.000

0000$6   06         0005      19.000

0000$6   06         0006      21.000

I have sorted this table by KNUMH KOPOS.

I expect my tables values to be sorted based on KNUMH KOPOS in Ascending order.

But my result is :::::

KNUMH KOPOS KLFN1 KSTBW

0000$1   01         0001      0.000

0000$1   01         0002      12.000

0000$1   01         0003      15.000

0000$1   01         0004      17.000

0000$1   01         0005      19.000

0000$1   01         0006      21.000

0000$2   02         0001      0.000

0000$2   02         0002      12.000

0000$2   02         0003      15.000

0000$2   02         0004      17.000

0000$2   02         0005      19.000

0000$2   02         0006      21.000

0000$3   03         0006      21.000    

0000$3   03         0005      19.000

0000$3   03         0004      17.000

0000$3   03         0003      15.000

0000$3   03         0002      12.000

0000$3   03         0001      0.000

0000$4   04         0001      0.000

0000$4   04         0002      12.000

0000$4   04         0003      15.000

0000$4   04         0004      17.000

0000$4   04         0005      19.000

0000$4   04         0006      21.000

0000$5   05         0001      0.000

0000$5   05         0002      12.000

0000$5   05         0003      15.000

0000$5   05         0004      17.000

0000$5   05         0005      19.000

0000$5   05         0006      21.000

0000$6   06         0001      0.000

0000$6   06         0002      12.000

0000$6   06         0003      15.000

0000$6   06         0004      17.000

0000$6   06         0005      19.000

0000$6   06         0006      21.000

The BOLD values are unexpected behaviour.

Can some one explain me why this is happening so ?

12 REPLIES 12
Read only

anilkumar_kalkivai
Active Participant
0 Likes
1,983

Hi Kishore,

Since, you have sorted only on the basis of KNUMH,KOPOS , So it the system will the records only the basis of these 2 fields.

While sorting records few records will shuffle their places and after sorting the system will not bother about the other fields order.

So, If you want output according to your expectation  sort on the basis of KNUMH,KOPOS,KLFN1.

Regards,

Anil.

Read only

Former Member
0 Likes
1,983

The sort is working properly.

It will arrange the records on fist found beyond the sort criteria

So in this case the system might be finding the records in the specified order in the itab.

if you want it to be stable use the stable clause.

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3800358411d1829f0000e829fbfe/content.htm

Read only

matt
Active Contributor
0 Likes
1,983

The SORT by default is NOT stable. You are only guaranteed the result if you use the addition STABLE. This is well documented.

Read only

Former Member
1,983

Try this.

SORT <ITAB> STABLE BY KNUMH KOPOS.

Read only

venkateswaran_k
Active Contributor
0 Likes
1,983

Probably you may have to add the third col as well.

SORT it_table by KNUMH   KOPOS   KLFN1

Regards,

Venkat

Read only

Former Member
0 Likes
1,983

Hi Kishore,

As mentioned by all experts here your sort is perfectly working fine as per your code.

Generic syntax is :

Sort <Internal Table> <field1> ascending/descending <field2> ascending/descending ....................

BR.

Read only

former_member209120
Active Contributor
0 Likes
1,983

Hi Kishore,

Sort your internal table by KNUMH KOPOS KLFN1.

Sort itab by KNUMH KOPOS KLFN1.

Regards,

Ramesh.T

Read only

Former Member
0 Likes
1,983

Thanks every one.

After I have found the inconsistency in sorting I have changed the code from

SORT table by KNUMH KOPOS KLFN1.

I want to know the reason why it is being sorted that way..?

If I know the reason I can code minding this type of behaviour in future when using sort statements.

Sorry, I could not mark given answers as Helpful/answered. Seems I don't have authorizations.

Read only

matt
Active Contributor
0 Likes
1,983

Did you not read my answer - it explained what was going on.

The problem you had was that the table was already ordered by KLFN1, but when you sorted your table by KNUMH, KOPOS, you lost that order.

The answer is

The SORT by default is NOT stable. You are only guaranteed the result if you use the addition STABLE. This is well documented.

SORT table by KNUMH KOPOS STABLE will keep the sort order of KLFN1. Having said that, relying on the data being partially in the right order is not safe, so it is better to include KLFN1 in the sort.

Look at the ABAP Help on SORT. It says:

Stable sort

The option

SORT itab... STABLE.

allows you to perform a stable sort, that is, the relative sequence of lines that are unchanged by the sort is not changed. If you do not use the STABLE option, the sort sequence is not preserved. If you sort a table several times by the same key, the sequence of the table entries will change in each sort. However, a stable sort takes longer than an unstable sort.

All clear now?

The reason that you mark helpful answers etc. is that this is such a basic question, easily answered by reading the documentation and easily worked around with a little thought, that I set it to a discussion.

Read only

0 Likes
1,983

Thanks a lot for the clarification.

I did not sort my table basing KLFN1, it was my default data of course it is in sorted order.

Does this happens every time, means the other columns order getting disturbed when I sort basing a few columns ?

In my 36 rows of data only row13 to row18 got sorted diffrently. Why not the other rows ?

Please do not mind if I am asking you small questions. I am trying to learn from you guys.

Thanks in advance.

Read only

matt
Active Contributor
0 Likes
1,983

Yes. That is EXACTLY what it mean. If you do not use STABLE, the relative position of records may be disturbed.

Now is it clear?

If you want to know why it is like this, try this wikipedia article: http://en.wikipedia.org/wiki/Sort_algorithm

Read only

0 Likes
1,983

Until now my thought was::: Sort statement will order the records based on the given columns with out disturbing other columns.

Thanks to every one specially Matthew for the clarification.

Can some one look into this please.

http://scn.sap.com/thread/3373873