July 07, 2005
The following example gives the wrong result:

# import std.stdio;
#
# struct A
# {
#   int i;
#   int opCmp(inout A a) { return i - a.i; }
# }
#
# void main()
# {
#   A[] l = new A[6];
#
#   l[0].i = 2;
#   l[1].i = 21;
#   l[2].i = 12;
#   l[3].i = 22;
#   l[4].i = 1;
#
#   l.sort;
#
#   foreach(int i, A a; l)
#     writefln(a.i);
# }
The output becomes 'sorted' in the same way as when opCmp is not present at all.

However, if the 'inout' is removed from opCmp, it works as expected. I use inout on large structs to avoid passing huge amounts of data on the stack, but I'm not sure if this is the 'correct' thing to do...

Note that explicit comparison, eg. l[2] <= l[3], works correctly both with and without the 'inout' present.

Nick


July 08, 2005
"Nick" <Nick_member@pathlink.com> wrote in message news:daitbq$tia$1@digitaldaemon.com...
> The following example gives the wrong result:
> [...]
> #   int opCmp(inout A a) { return i - a.i; }

rewrite that line as:

    int opCmp(A* a) { return i - a.i; }

and you'll get the behavior you're looking for. The sorter looks for either an

    in A

parameter or an:

    in A*

parameter for the opCmp.