From 983d364845615f4f11b820069726e72460b32ce2 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Mon, 23 Jan 2023 23:38:47 +0700 Subject: [PATCH] Fixed array multi sorting --- admin/admin_extensions.php | 2 +- .../attach_mod/includes/functions_admin.php | 46 +++---------------- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/admin/admin_extensions.php b/admin/admin_extensions.php index f40e548d9..a06744841 100644 --- a/admin/admin_extensions.php +++ b/admin/admin_extensions.php @@ -202,7 +202,7 @@ if ($mode == 'extensions') { DB()->sql_freeresult($result); if ($num_extension_row > 0) { - $extension_row = sort_multi_array($extension_row, 'group_name', 'ASC'); + $extension_row = sort_multi_array($extension_row, 'extension'); for ($i = 0; $i < $num_extension_row; $i++) { if ($submit) { diff --git a/library/attach_mod/includes/functions_admin.php b/library/attach_mod/includes/functions_admin.php index ec8e8a2df..a61c71b8a 100644 --- a/library/attach_mod/includes/functions_admin.php +++ b/library/attach_mod/includes/functions_admin.php @@ -82,47 +82,15 @@ function process_quota_settings($mode, $id, $quota_type, $quota_limit_id = 0) /** * Sort multi-dimensional array * - * @param $sort_array - * @param $key - * @param $sort_order - * @param int $pre_string_sort - * @return mixed + * @param array $sort_array + * @param string|int $key + * @param int $sort_order + * @return array */ -function sort_multi_array($sort_array, $key, $sort_order, $pre_string_sort = 0) +function sort_multi_array(array $sort_array, $key, int $sort_order = SORT_ASC): array { - $last_element = count($sort_array) - 1; - - if (!$pre_string_sort) { - $string_sort = (!is_numeric(@$sort_array[$last_element - 1][$key])) ? true : false; - } else { - $string_sort = $pre_string_sort; - } - - for ($i = 0; $i < $last_element; $i++) { - $num_iterations = $last_element - $i; - - foreach ($sort_array as $j => $jValue) { - // do checks based on key - $switch = false; - if (!$string_sort) { - if (($sort_order == 'DESC' && (int)(@$sort_array[$j][$key]) < (int)(@$sort_array[$j + 1][$key])) || - ($sort_order == 'ASC' && (int)(@$sort_array[$j][$key]) > (int)(@$sort_array[$j + 1][$key]))) { - $switch = true; - } - } else { - if (($sort_order == 'DESC' && strcasecmp(@$sort_array[$j][$key], @$sort_array[$j + 1][$key]) < 0) || - ($sort_order == 'ASC' && strcasecmp(@$sort_array[$j][$key], @$sort_array[$j + 1][$key]) > 0)) { - $switch = true; - } - } - - if ($switch) { - $temp = $jValue; - $sort_array[$j] = $sort_array[$j + 1]; - $sort_array[$j + 1] = $temp; - } - } - } + $keys = array_column($sort_array, $key); + array_multisort($keys, $sort_order, $sort_array); return $sort_array; }