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

Gide Nwawudu gide at btinternet.com
Wed Sep 17 17:43:08 PDT 2008


I think this is the same bug as 1309.  If so, you should add your test
case to the comments.

http://d.puremagic.com/issues/show_bug.cgi?id=1309


Gide

On Tue, 02 Sep 2008 18:09:27 -0400, Sivo Schilling
<sivo.schilling at web.de> wrote:

>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