2013 Jun 29 11:43 AM
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 ?
2013 Jun 29 1:37 PM
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.
2013 Jun 29 1:42 PM
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
2013 Jun 29 7:08 PM
The SORT by default is NOT stable. You are only guaranteed the result if you use the addition STABLE. This is well documented.
2013 Jun 29 3:04 PM
2013 Jun 29 3:12 PM
Probably you may have to add the third col as well.
SORT it_table by KNUMH KOPOS KLFN1
Regards,
Venkat
2013 Jun 29 6:30 PM
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.
2013 Jul 01 5:01 AM
Hi Kishore,
Sort your internal table by KNUMH KOPOS KLFN1.
Sort itab by KNUMH KOPOS KLFN1.
Regards,
Ramesh.T
2013 Jul 01 5:46 AM
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.
2013 Jul 01 6:47 AM
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:
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.
2013 Jul 01 8:01 AM
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.
2013 Jul 01 8:39 AM
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
2013 Jul 01 8:48 AM
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.