.sort property for array of structs broken in DMD 2 ?

Sivo Schilling sivo.schilling at web.de
Tue Sep 2 15:09:27 PDT 2008


Below a small program testing the .sort property for an array of
structs. 

---
// Test of opCmp for structs

import std.stdio;
import std.string;

struct S
{
    string face;

    int opCmp(S rhs)
    {
        int fcmp = cmp(face, rhs.face);
        return fcmp;
    }
}

void testOpCmp()
{
    S[] faces = new S[5];

    faces[0].face = "Morgens";
    faces[1].face = "Mittags";
    faces[2].face = "Abends";
    faces[3].face = "Nachts";
    faces[4].face = "Immer";

    writefln("faces before .sort");
    foreach(int i, S f; faces) writefln("i = %d, face = %s", i, f.face);

    faces = faces.sort;

    writefln("faces after .sort");
    foreach(int i, S f; faces) writefln("i = %d, face = %s", i, f.face);
}

void main()
{
    testOpCmp();
}
---
The output of the program compling with DMD 1.034 is as expected:

faces before .sort
i = 0, face = Morgens
i = 1, face = Mittags
i = 2, face = Abends
i = 3, face = Nachts
i = 4, face = Immer

faces after .sort
i = 0, face = Abends
i = 1, face = Immer
i = 2, face = Mittags
i = 3, face = Morgens
i = 4, face = Nachts

But compiling with DMD 2.018 without any modification of the code
the program outputs:

 faces before .sort
i = 0, face = Morgens
i = 1, face = Mittags
i = 2, face = Abends
i = 3, face = Nachts
i = 4, face = Immer

faces after .sort
i = 0, face = Mittags
i = 1, face = Morgens
i = 2, face = Nachts
i = 3, face = Abends
i = 4, face = Immer

The output of course indicates that the opCmp member of S is completly
ignored and that realy happens in this case (easy to proof).

But then the .sort property at least for arrays of structs is not much
useful.

Any ideas ?

Regards.


More information about the Digitalmars-d-learn mailing list