Skip to content

Commit

Permalink
moved sortArrayByKey logic into Utils::
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Apr 9, 2018
1 parent 1ec6532 commit 47037e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
19 changes: 4 additions & 15 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,25 +371,14 @@ public function base64DecodeFilter($str)
*
* @param array $input
* @param string $filter
* @param array|int $direction
* @param int $direction
* @param int $sort_flags
*
* @return array
*/
public function sortByKeyFilter($input, $filter, $direction = SORT_ASC)
public function sortByKeyFilter($input, $filter, $direction = SORT_ASC, $sort_flags = SORT_REGULAR)
{
$output = [];

if (!is_array($input) || !$input) {
return $output;
}

foreach ($input as $key => $row) {
$output[$key] = $row[$filter];
}

array_multisort($output, $direction, $input);

return $input;
return Utils::sortArrayByKey($input, $filter, $direction, $sort_flags);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,32 @@ public static function sortArrayByArray(array $array, array $orderArray)
return $ordered + $array;
}

/**
* Sort an array by a key value in the array
*
* @param $array
* @param $array_key
* @param int $direction
* @param int $sort_flags
* @return array
*/
public static function sortArrayByKey($array, $array_key, $direction = SORT_DESC, $sort_flags = SORT_REGULAR )
{
$output = [];

if (!is_array($array) || !$array) {
return $output;
}

foreach ($array as $key => $row) {
$output[$key] = $row[$array_key];
}

array_multisort($output, $direction, $sort_flags, $array);

return $array;
}

/**
* Get's path based on a token
*
Expand Down

0 comments on commit 47037e3

Please sign in to comment.