cmp

Performs a lexicographical comparison on two input ranges. Iterating r1 and r2 in lockstep, cmp compares each element e1 of r1 with the corresponding element e2 in r2. If one of the ranges has been finished, cmp returns a negative value if r1 has fewer elements than r2, a positive value if r1 has more elements than r2, and 0 if the ranges have the same number of elements.

If the ranges are strings, cmp performs UTF decoding appropriately and compares the ranges one code point at a time.

A custom predicate may be specified, in which case cmp performs a three-way lexicographical comparison using pred. Otherwise the elements are compared using opCmp.

  1. auto cmp(R1 r1, R2 r2)
    cmp
    (
    R1
    R2
    )
    (
    R1 r1
    ,
    R2 r2
    )
  2. int cmp(R1 r1, R2 r2)

Parameters

r1 R1

The first range.

r2 R2

The second range.

Return Value

Type: auto

0 if the ranges compare equal. A negative value if r1 is a prefix of r2 or the first differing element of r1 is less than the corresponding element of r2 according to pred. A positive value if r2 is a prefix of r1 or the first differing element of r2 is less than the corresponding element of r1 according to pred.

Note: An earlier version of the documentation incorrectly stated that -1 is the only negative value returned and 1 is the only positive value returned. Whether that is true depends on the types being compared.

Examples

1 int result;
2 
3 result = cmp("abc", "abc");
4 assert(result == 0);
5 result = cmp("", "");
6 assert(result == 0);
7 result = cmp("abc", "abcd");
8 assert(result < 0);
9 result = cmp("abcd", "abc");
10 assert(result > 0);
11 result = cmp("abc"d, "abd");
12 assert(result < 0);
13 result = cmp("bbc", "abc"w);
14 assert(result > 0);
15 result = cmp("aaa", "aaaa"d);
16 assert(result < 0);
17 result = cmp("aaaa", "aaa"d);
18 assert(result > 0);
19 result = cmp("aaa", "aaa"d);
20 assert(result == 0);
21 result = cmp("aaa"d, "aaa"d);
22 assert(result == 0);
23 result = cmp(cast(int[])[], cast(int[])[]);
24 assert(result == 0);
25 result = cmp([1, 2, 3], [1, 2, 3]);
26 assert(result == 0);
27 result = cmp([1, 3, 2], [1, 2, 3]);
28 assert(result > 0);
29 result = cmp([1, 2, 3], [1L, 2, 3, 4]);
30 assert(result < 0);
31 result = cmp([1L, 2, 3], [1, 2]);
32 assert(result > 0);

Example predicate that compares individual elements in reverse lexical order

1 int result;
2 
3 result = cmp!"a > b"("abc", "abc");
4 assert(result == 0);
5 result = cmp!"a > b"("", "");
6 assert(result == 0);
7 result = cmp!"a > b"("abc", "abcd");
8 assert(result < 0);
9 result = cmp!"a > b"("abcd", "abc");
10 assert(result > 0);
11 result = cmp!"a > b"("abc"d, "abd");
12 assert(result > 0);
13 result = cmp!"a > b"("bbc", "abc"w);
14 assert(result < 0);
15 result = cmp!"a > b"("aaa", "aaaa"d);
16 assert(result < 0);
17 result = cmp!"a > b"("aaaa", "aaa"d);
18 assert(result > 0);
19 result = cmp!"a > b"("aaa", "aaa"d);
20 assert(result == 0);
21 result = cmp("aaa"d, "aaa"d);
22 assert(result == 0);
23 result = cmp!"a > b"(cast(int[])[], cast(int[])[]);
24 assert(result == 0);
25 result = cmp!"a > b"([1, 2, 3], [1, 2, 3]);
26 assert(result == 0);
27 result = cmp!"a > b"([1, 3, 2], [1, 2, 3]);
28 assert(result < 0);
29 result = cmp!"a > b"([1, 2, 3], [1L, 2, 3, 4]);
30 assert(result < 0);
31 result = cmp!"a > b"([1L, 2, 3], [1, 2]);
32 assert(result > 0);

Meta

Suggestion Box / Bug Report