From 47037e3f5ee262d34dd673db74c3e5ad4235c169 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 9 Apr 2018 14:35:44 -0600 Subject: [PATCH] moved sortArrayByKey logic into `Utils::` --- system/src/Grav/Common/Twig/TwigExtension.php | 19 +++----------- system/src/Grav/Common/Utils.php | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 6de7e106b..fb27ec823 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -371,25 +371,14 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn * * @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); } /** diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 9193a6708..2736fd904 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -966,6 +966,32 @@ abstract class Utils 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 *