Search
View source code
Display the source code in std/algorithm/sorting.d from which this page was generated on github.
Report a bug
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.sorting.ordered

Like isSorted, returns true if the given values are ordered according to the comparison operation less. Unlike isSorted, takes values directly instead of structured in a range.

bool ordered(alias less, T...) (
T values
)
if (T.length == 2 && is(typeof(binaryFun!less(values[1], values[0])) : bool) || T.length > 2 && is(typeof(ordered!less(values[0..1 + __dollar / 2]))) && is(typeof(ordered!less(values[__dollar / 2..__dollar]))));

ordered allows repeated values, e.g. ordered(1, 1, 2) is true. To verify that the values are ordered strictly monotonically, use strictlyOrdered; strictlyOrdered(1, 1, 2) is false.

With either function, the predicate must be a strict ordering. For example, using "a <= b" instead of "a < b" is incorrect and will cause failed assertions.

Parameters

NameDescription
values The tested value
less The comparison predicate

Returns

true if the values are ordered; ordered allows for duplicates, strictlyOrdered does not.

Example

assert(ordered(42, 42, 43));
assert(!strictlyOrdered(43, 42, 45));
assert(ordered(42, 42, 43));
assert(!strictlyOrdered(42, 42, 43));
assert(!ordered(43, 42, 45));
// Ordered lexicographically
assert(ordered("Jane", "Jim", "Joe"));
assert(strictlyOrdered("Jane", "Jim", "Joe"));
// Incidentally also ordered by length decreasing
assert(ordered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
// ... but not strictly so: "Jim" and "Joe" have the same length
assert(!strictlyOrdered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));