/** * Sort elements in this iterable by their properties or array values. * * $properties - A mapping between field names and sort direction (1 or -1). The order of keys * determines the precedence of a field in determining the relative ordering of * two objects. * * Examples * * Crankshaft::Iter([['a' => 30], ['a' => 10], ['a' => 20]])->sort_by_properties(['a' => 1]) * // => ['a' => 10], ['a' => 20], ['a' => 30] * * Crankshaft::Iter([['a' => 30], ['a' => 10], ['a' => 20]])->sort_by_properties(['a' => -1]) * // => ['a' => 30], ['a' => 20], ['a' => 10] * * Crankshaft::Iter([['a' => 2, 'b' => 3], ['a' => 1, 'b' => 2], ['a' => 1, 'b' => 1]]) * ->sort_by_properties(['a' => 1, 'b' => -1]) * // => ['a' => 1, 'b' => 2], ['a' => 1, 'b' => 1], ['a' => 2, 'b' => 3] * * See also * * pluck() - For the requirements placed on the elements in this iterable. * sort() - For properties of the sort. * * Returns an Iterable */ public function sort_by_properties(array $properites) { return $this->sort(function ($a, $b) use($properites) { foreach ($properites as $property => $direction) { $a_value = $this->pluck_property($a, $property); $b_value = $this->pluck_property($b, $property); $ordering = Crankshaft::compare($a_value, $b_value); if ($ordering !== 0) { return $direction * $ordering; } } return 0; }); }