diff --git a/app/models/Utilities.php b/app/models/Utilities.php index 9beac1c..a5a81af 100644 --- a/app/models/Utilities.php +++ b/app/models/Utilities.php @@ -178,4 +178,29 @@ class Utilities return $result; } + + /** + * Sort Array by keys + * + * @param array $data + * @param [type] $key + * @param string $operator ('asc'/'desc') + * @return void + */ + static function sortArrayByKey($data = [], $key, $operator = "asc"){ + if ($operator == "asc") + { + uasort($data, function($a, $b) use ($key){ + if ($a[$key] == $b[$key]) return 0; + return ($a[$key] < $b[$key]) ? -1 : 1; + }); + } else{ + uasort($data, function($a, $b) use ($key){ + if ($a[$key] == $b[$key]) return 0; + return ($a[$key] > $b[$key]) ? -1 : 1; + }); + } + return $data; + + } } diff --git a/app/views/Device.php b/app/views/Device.php index 8b809d2..502613c 100644 --- a/app/views/Device.php +++ b/app/views/Device.php @@ -103,23 +103,15 @@ class Device extends Template if (!empty ($_GET['sort']) && !empty ($_GET['sortType']) && $_GET['sort'] == "firmware") { if ($_GET['sortType'] == "DESC") { - usort($devices, function($a, $b) { - return $a['firmware_hash'] <=> $b['firmware_hash']; - }); + return Utilities::sortArrayByKey($devices, 'firmware_hash', "desc"); } else if ($_GET['sortType'] == "ASC") { - usort($devices, function($a, $b) { - return $b['firmware_hash'] <=> $a['firmware_hash']; - }); + return Utilities::sortArrayByKey($devices, 'firmware_hash', "asc"); } } else if (!empty ($_GET['sort']) && !empty ($_GET['sortType']) && $_GET['sort'] == "signal") { if ($_GET['sortType'] == "DESC") { - usort($devices, function($a, $b) { - return $a['signal'] <=> $b['signal']; - }); + return Utilities::sortArrayByKey($devices, 'signal', "desc"); } else if ($_GET['sortType'] == "ASC") { - usort($devices, function($a, $b) { - return $b['signal'] <=> $a['signal']; - }); + return Utilities::sortArrayByKey($devices, 'signal', "asc"); } } diff --git a/app/views/Plugins.php b/app/views/Plugins.php index d3bcf03..b1b4e68 100644 --- a/app/views/Plugins.php +++ b/app/views/Plugins.php @@ -21,8 +21,8 @@ class Plugins extends Template $plugins[$key]['status'] = $status; } - sort($plugins); - + $plugins = Utilities::sortArrayByKey($plugins, 'status', "desc"); + $template = new Template('plugins'); $template->prepare('baseDir', BASEDIR); $template->prepare('debugMod', DEBUGMOD);