30 April 2021

"SORT" is not stable !

Once in a while, you find something that challenge everything you thought you knew about a given subject. In may case I just found the the sort order using the default SORT ... BY X Y is not stable. That is, if you have several element with the same X and Y, they can be sorted in any order, and there is no guaranty that they will be sorted in the same way by consecutive sorts.

The case

I have two structures. The first one contains 5 fields, while the second one contains the same 5 fields, alongside with several others.

TYPES:
  BEGIN OF ty_cs_dlin_1,
    dlinr      TYPE ad01dlinr,
    dlius      type AD01USAGE,
    vbeln      TYPE vbeln,
    vbpos      TYPE posnr,
    in_wtgbtr  TYPE eb_fkwrt,
  END OF ty_cs_dlin _1.

TYPES:
  BEGIN OF ty_cs_dlin_2,
    dlinr      TYPE ad01dlinr,
    dlius      type AD01USAGE,
    vbeln      TYPE vbeln,
    vbpos      TYPE posnr,
    in_wtgbtr  TYPE eb_fkwrt,
    dopen      TYPE ad01dopensign,
    objnr      TYPE j_objnr,
    twaer      TYPE twaer,
    ... several other fields
  END OF ty_cs_dlin_2.

Those structure are used to instantiate two tables. Initially, the two tables are sorted in the same order.

DATA lt_table1 TYPE STANDARD TABLE OF ty_cs_dlin_1.
DATA lt_table2 TYPE STANDARD TABLE OF ty_cs_dlin_2.

LT_TABLE1
DLINR DLIUS VBELN VBPOS IN_WTGBTR
24932033 1 0071088109 20 2 596,00
24932034 1 0071088109 200 15,50
24932035 1 0071088109 190 358,04
24932036 1 0071088109 170 18,44
24932037 1 0071088109 150 113,39
24932038 1 0071088109 180 2,51
24932039 1 0071088109 180 2,51
24932040 1 0071088109 170 32,48
24932061 1 0071088109 180 2,51
24932062 1 0071088109 180 3,03

LT_TABLE2
DLINR DLIUS VBELN VBPOS IN_WTGBTR DOPEN OBJNR
24932033 1 0071088109 20 2 596,00 PR02662765
24932034 1 0071088109 200 15,50 PR02662765
24932035 1 0071088109 190 358,04 PR02662765
24932036 1 0071088109 170 18,44 PR02662765
24932037 1 0071088109 150 113,39 PR02662765
24932038 1 0071088109 180 2,51 PR02662765
24932039 1 0071088109 180 2,51 PR02662765
24932040 1 0071088109 170 32,48 PR02662765
24932061 1 0071088109 180 2,51 PR02662765

Then we sort them by VBELN and VBPOS.

SORT lt_table1 BY vbeln vbpos.
SORT lt_table2 BY vbeln vbpos.

You would think that the result would show the two tables still in the same order : we start from a similar state and perform the same operation...

Alas, this is not the case...

LT_TABLE1
DLINR DLIUS VBELN VBPOS IN_WTGBTR
24932140 1 0071088109 10 7 950,00
24932138 1 0071088109 20 508 612,50
24932089 1 0071088109 20 2 911,00
24932088 1 0071088109 20 169 537,50
24932086 1 0071088109 20 1 443,79
24932033 1 0071088109 20 2 596,00
24932139 1 0071088109 30 23 339,20
24932128 1 0071088109 50 97,93
24932116 1 0071088109 50 194,40

LT_TABLE2
DLINR DLIUS VBELN VBPOS IN_WTGBTR DOPEN OBJNR
24932140 1 0071088109 10 7 950,00 PR02662765
24932089 1 0071088109 20 2 911,00 PR02662765
24932088 1 0071088109 20 169 537,50 PR02662765
24932138 1 0071088109 20 508 612,50 PR02662765
24932086 1 0071088109 20 1 443,79 PR02662765
24932033 1 0071088109 20 2 596,00 PR02662765
24932139 1 0071088109 30 23 339,20 PR02662765
24932126 1 0071088109 50 172,97 PR02662765
24932125 1 0071088109 50 377,87 PR02662765

I must admit that this took me by surprise, and it took me some time to debug my program to the SORT operation, and before having to admit that, yes, the sort did not give the same results for our two tables.

The cause

Searching the documentation, it is clear that this is a normal (should I say "expected " ? ) results. It is clearly said that the sort is not stable, and that two sort may give different results on the order of non sorted key fields.

Sorting is unstable by default, which means that the relative order of rows that do not have different sort keys is not preserved when they are sorted, and can change depending on the platform or when a sort is performed multiple times. The addition STABLE can be used for stable sorting.

If you fully read this, you can see that multiples sort of the same table can give different results. 

In my tests, I could not manage to get such a thing to occurs, but as our case shown, different structures do sort differently.

Adding the STABLE keyword at the end of the sort garantie that the order of the line in the initial and sorted table will be the same (for identical sorting fields, of course).


No comments:

Post a Comment