View source code
Display the source code in std/algorithm/sorting.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
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.isSorted

Checks whether a forward range is sorted according to the comparison operation less. Performs Ο(r.length) evaluations of less.

bool isSorted(alias less, Range) (
  Range r
)
if (isForwardRange!Range);

Unlike isSorted, isStrictlyMonotonic does not allow for equal values, i.e. values for which both less(a, b) and less(b, a) are false.

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

Parameters

NameDescription
less Predicate the range should be sorted by.
r Forward range to check for sortedness.

Returns

true if the range is sorted, false otherwise. isSorted allows duplicates, isStrictlyMonotonic not.

Example

assert([1, 1, 2].isSorted);
// strictly monotonic doesn't allow duplicates
assert(![1, 1, 2].isStrictlyMonotonic);

int[] arr = [4, 3, 2, 1];
assert(!isSorted(arr));
assert(!isStrictlyMonotonic(arr));

assert(isSorted!"a > b"(arr));
assert(isStrictlyMonotonic!"a > b"(arr));

sort(arr);
assert(isSorted(arr));
assert(isStrictlyMonotonic(arr));

Authors

Andrei Alexandrescu

License

Boost License 1.0.