www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - .sort property for array of structs broken in DMD 2 ?

reply Sivo Schilling <sivo.schilling web.de> writes:
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.
Sep 02 2008
next sibling parent Extrawurst <spam extrawurst.org> writes:
Did u file a bugreport ? If there is not already one for this, cause i 
think it is a known issue


Sivo Schilling 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.

Sep 02 2008
prev sibling parent Gide Nwawudu <gide btinternet.com> writes:
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 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.

Sep 17 2008