[Issue 3249] New: sort and setIntersection on array of struct or class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Aug 12 16:15:29 PDT 2009


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 at puremagic.com
        ReportedBy: Jesse.K.Phillips+D at gmail.com
                CC: Jesse.K.Phillips+D at 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: -------


More information about the Digitalmars-d-bugs mailing list