Misleading documentation of sort/4

Hi

The documention of sort/4 sort/4

says:

The following example sorts a list of rows, for example resulting from csv_read_file/2) ascending on the 3th column and descending on the 4th column:

sort(4, @>=, Rows0, Rows1), sort(3, @=<, Rows1, Sorted).

But that is wrong isn’t it? The first sort will sort on column 4 then the second sort will sort on column 3 i.e. only the second sort will have any effect.

The following illustrates:

test :-
    X1 = [row(3, a), row(1, b), row(2, c), row(1, c), row(1, a)],
    sort(1, @=<, X1, X2),
    sort(2, @=<, X2, X3),
    write(X3).

:- test.
[row(1,a),row(3,a),row(1,b),row(1,c),row(2,c)]
true.
2 Likes

| mike.elston
January 4 |

  • | - |

Hi

The documention of sort/4 sort/4

says:

The following example sorts a list of rows, for example resulting from csv_read_file/2) ascending on the 3th column and descending on the 4th column:

sort(4, @>=, Rows0, Rows1), sort(3, @=<, Rows1, Sorted).

But that is wrong isn’t it? The first sort will sort on column 4 then the second sort will sort on column 3 i.e. only the second sort will have any effect.

Must read as: “ascending on the 3th column and descending on the 4th column (when 3rd column is equal)”
That is, the exampe wants to show that the sort is actually stable.

https://stackoverflow.com/questions/1517793/what-is-stability-in-sorting-algorithms-and-why-is-it-important:

Now, the bug is that it must have said 3rd, not 3th :slight_smile:

Cheers

2 Likes

Thanks. Pushed DOC: sort/4 stable sort example. · SWI-Prolog/swipl-devel@54fdcbe · GitHub

1 Like