Little Tweeks + Sort Function for array

This commit is contained in:
GamerClassN7 2020-11-03 16:29:10 +01:00
parent a4cdb250b9
commit 5ad11cbb10
3 changed files with 31 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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);