Requires the Hack Standard Library to be installed.
Facebook Engineer?

This function is available as C\is_sorted() in Facebook's www repository.

Returns true if the given Traversable is sorted in ascending order

namespace HH\Lib\C;

function is_sorted<Tv>(
  Traversable<Tv> $traversable,
  ?(function(Tv, Tv): num) $comparator = NULL,
): bool;

If two neighbouring elements compare equal, this will be considered sorted.

If no $comparator is provided, the <=> operator will be used. This will sort numbers by value, strings by alphabetical order or by the numeric value, if the strings are well-formed numbers, and DateTime/DateTimeImmutable by their unixtime.

To check the order of other types or mixtures of the aforementioned types, see C\is_sorted_by.

If the comparison operator <=> is not useful on Tv and no $comparator is provided, the result of is_sorted will not be useful.

Time complexity: O((n * c), where c is the complexity of the comparator function (which is O(1) if not provided explicitly) Space complexity: O(n)



  • bool