/** * Sorts items by a value of "position" attribute * * @param array $items The array to be processed */ protected function sortItemsByPosition(array &$items) { ArrayUtils::sortBy($items, false, 'position'); foreach ($items as &$item) { unset($item['position']); } }
/** * Sorts an array by specified property. * * This method uses the stable sorting algorithm. See http://en.wikipedia.org/wiki/Sorting_algorithm#Stability * * Supported options: * property [string] The path of the property by which the array should be sorted. Defaults to 'priority' * reverse [boolean] Indicates whether the sorting should be performed in reverse order. Defaults to FALSE * sorting-type [string] number, string or string-case (for case-insensitive sorting). Defaults to 'number' * * @param array $array The array to be sorted * @param array $options The sorting options * * @return array The sorted array */ public function sortBy(array $array, array $options = []) { $sortingType = self::getOption($options, 'sorting-type', 'number'); if ($sortingType === 'number') { $sortingFlags = SORT_NUMERIC; } else { $sortingFlags = SORT_STRING; if ($sortingType === 'string-case') { $sortingFlags |= SORT_FLAG_CASE; } } ArrayUtils::sortBy($array, self::getOption($options, 'reverse', false), self::getOption($options, 'property', 'priority'), $sortingFlags); return $array; }
/** * Sorts the given items by 'order' attribute * * @param array $items * @return mixed */ protected function sortItems($items) { ArrayUtils::sortBy($items, false, 'order'); return array_keys($items); }
protected function sortBlocks() { ArrayUtils::sortBy($this->blocks, true); }
/** * @param array $calendars */ protected function normalizeCalendarData(array &$calendars) { // apply default values and remove redundant properties $defaultValues = $this->getCalendarDefaultValues(); foreach ($calendars as &$calendar) { $this->applyCalendarDefaultValues($calendar, $defaultValues); } ArrayUtils::sortBy($calendars, false, 'position'); }
public function testSortByObjectPath() { $obj1 = $this->createObject(['name' => '1', 'child' => $this->createObject(['priority' => null])]); $obj2 = $this->createObject(['name' => '2', 'child' => $this->createObject(['priority' => 100])]); $obj3 = $this->createObject(['name' => '3', 'child' => $this->createObject(['priority' => 0])]); $array = [$obj1, $obj2, $obj3]; ArrayUtils::sortBy($array, false, 'child.priority'); $this->assertSame([$obj1, $obj3, $obj2], $array); }