sort using delegate as predicate

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 16 09:21:54 PDT 2014


On Tue, 16 Sep 2014 16:19:10 +0000, Simon Bürger wrote:

> 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?

Bit of a workaround, but this should do it:

   sort!((a,b) => f.cmp(a, b))(data);


More information about the Digitalmars-d-learn mailing list