std.algorithm.cmp doesn't seem to support numeric types?

Adam D. Ruppe destructionator at gmail.com
Wed Oct 16 20:07:10 UTC 2019


On Wednesday, 16 October 2019 at 19:25:18 UTC, DNoob wrote:
> I'm just learning D, so it's very possible that I'm missing a 
> more appropriate function that exists elsewhere, but basically 
> I found today that while I could easily write a cmp function 
> that worked for everything, the one in std.algorithm doesn't 
> seem to:

Yeah, it is primarily for strings, to be used as a sorting 
predicate.

For numbers, simple less than (for bool comparisons) or 
subtraction operators (for the integer ones) will do it.

Notice that the docs say "a negative value" rather than -1 
specifically. That's because the implementation for integers an 
be as simple as

return a - b; // if b > a, you get a negative value

and thus doesn't really need a specific function.


More information about the Digitalmars-d-learn mailing list