Comparator

Members

Functions

compare
int compare(in S a, in T b)

Return comparison value akin to opCmp.

eq
bool eq(in S a, in T b)

Return true iff a == b.

ge
bool ge(in S a, in T b)

Return true iff a >= b.

gt
bool gt(in S a, in T b)

Return true iff a > b.

le
bool le(in S a, in T b)

Return true iff a <= b.

lt
bool lt(in S a, in T b)

Return true iff a < b.

Examples

1 alias compareSquares = Comparator!"a ^^ 2".compare;
2 
3 assert(compareSquares(1, 2) < 0);
4 assert(compareSquares(1, -2) < 0);
5 assert(compareSquares(-1, 1) == 0);
6 assert(compareSquares(-2.0, 1) > 0);
7 
8 alias compareByLength = Comparator!"a.length".compare;
9 
10 assert(compareByLength([], [1]) < 0);
11 assert(compareByLength([1, 2], [1]) > 0);
12 assert(compareByLength([1, 2], ["1", "2"]) == 0);
13 
14 alias compareAbsInts = Comparator!("a > 0 ? a : -a").compare!(int);
15 
16 assert(compareSquares(1, 2) < 0);
17 assert(compareSquares(1, -2) < 0);
18 assert(compareSquares(-1, 1) == 0);
19 assert(compareSquares(-2, 1) > 0);
20 static assert(!__traits(compiles, compareAbsInts(-2.0, 1.0)));
21 
22 alias ltSquared = Comparator!("a ^^ 2").lt;
23 
24 assert(ltSquared(1, 2));
25 assert(ltSquared(1, -2));
26 assert(!ltSquared(-2, -1));
27 
28 alias eqSquared = Comparator!("a ^^ 2").eq;
29 
30 assert(eqSquared(1, 1));
31 assert(eqSquared(1, -1));
32 assert(!eqSquared(1, 2));

Meta