www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3249] New: sort and setIntersection on array of struct or class

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3249

           Summary: sort and setIntersection on array of struct or class
           Product: D
           Version: 2.031
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: Jesse.K.Phillips+D gmail.com
                CC: Jesse.K.Phillips+D gmail.com


If you create an array of struct or class, sorting does not work for
std.algorthm.sort

The error from using sort:

    C:\opt\dmd\windows\bin\..\..\src\phobos\std\algorithm.d(3620): Error:
static assert  "Invalid predicate passed to sort: a < b"

The error from using setIntersection:

    C:\opt\dmd\windows\bin\..\..\src\phobos\std\functional.d(191): Error:
static assert  "Bad binary function q{a < b}. You need to use a valid D
expression using symbols a of type S and b of type S."

The code used:

    import std.algorithm;
    import std.stdio;

    struct S {
        string label;

        int opCmp(S s2) {
            if(label < s2.label)
                return -1;
            if(label > s2.label)
                return 1;
            else return 0;
        }
    }

    void main() {
        auto s1 = new S[2];
        auto s2 = new S[2];

        s1[0].label = "fish";
        s1[1].label = "bar";
        s2[0].label = "foo";
        s2[1].label = "fish";

        // Comment out to get setInterseciton error
        sort(s1);

        foreach(str; setIntersection(s1,s2))
            writeln(str);
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 12 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3249


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com





16:21:53 PDT ---
This is because the default comparison is passed as a string, which does not
see the definition of the struct. I'll change that to a function.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 12 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3249


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED




-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 28 2009