sort using delegate as predicate

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 16 09:19:10 PDT 2014


The following code does not compile, because the custom predicate 
of std.algorithm.sort is a template parameter, and therefore can 
only be a function, but not a delegate. In C++, there is a 
variant of sort taking a function-object as a second (run-time) 
parameter, but phobos does not seems to have such a thing. It 
seems like a pretty common problem to me, so does anyone have a 
solution?



class Foo
{
   int[] order;

   bool cmp(int a, int b) const
   {
     return order[a] < order[b];
   }
}

void main()
{
   auto f = new Foo;
   int[] data;
   sort!(f.cmp)(data);
}


More information about the Digitalmars-d-learn mailing list