HH\Lib\C\is_sorted_by

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

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

Returns true if the given Traversable would be sorted in ascending order after having been Vec\maped with $scalar_func sorted in ascending order

namespace HH\Lib\C;

function is_sorted_by<Tv, Ts>(
  Traversable<Tv> $traversable,
  (function(Tv): Ts) $scalar_func,
  ?(function(Ts, Ts): 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 without a mapping function, see C\is_sorted.

If the comparison operator <=> is not useful on Ts and no $comparator is provided, the result of is_sorted_by 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)

Parameters

Returns

  • bool