Files

134 lines
4.1 KiB
PHP

<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/**
* usort_data_query_index - attempts to sort a data query index either numerically
* or alphabetically depending on which seems best. it also tries to strip out
* extra characters before sorting to improve accuracy when sorting things like
* switch ifNames, etc
*
* @param string $a
* @param string $b
*
* @return int '1' if $a is greater than $b, '-1' if $a is less than $b, or '0' if
*/
function usort_data_query_index(string $a, string $b) : int {
/* split strings to be compared into chunks
* that shall be compared separately,
* e.g. for gi0/1, gi0/2, ... */
$arr_a = explode('/', $a);
$arr_b = explode('/', $b);
for ($i = 0; $i < min(cacti_count($arr_a), cacti_count($arr_b)); $i++) {
if ((is_numeric($arr_a[$i])) && (is_numeric($arr_b[$i]))) {
if (intval($arr_a[$i]) > intval($arr_b[$i])) {
return 1;
}
if (intval($arr_a[$i]) < intval($arr_b[$i])) {
return -1;
}
} else {
$cmp = strcmp(strval($arr_a[$i]), strval($arr_b[$i]));
if (($cmp > 0) || ($cmp < 0)) {
return $cmp;
}
}
}
if (cacti_count($arr_a) < cacti_count($arr_b)) {
return 1;
}
if (cacti_count($arr_a) > cacti_count($arr_b)) {
return -1;
}
return 0;
}
/**
* usort_numeric - sorts two values numerically (ie. 1, 34, 36, 76)
*
* @param string $a
* @param string $b
*
* @return int - '1' if $a is greater than $b, '-1' if $a is less than $b, or '0' if
*/
function usort_numeric(string $a, string $b) : int {
if (intval($a) > intval($b)) {
return 1;
}
if (intval($a) < intval($b)) {
return -1;
} else {
return 0;
}
}
/**
* usort_alphabetic - sorts two values alphabetically (ie. ab, by, ef, xy)
*
* @param string $a
* @param string $b
*
* @return int - '1' if $a is greater than $b, '-1' if $a is less than $b, or '0' if
*/
function usort_alphabetic(string $a, string $b) : int {
return strcmp($a, $b);
}
/**
* usort_natural - sorts two values naturally (ie. ab1, ab2, ab7, ab10, ab20)
*
* @param string $a
* @param string $b
*
* @return int - '1' if $a is greater than $b, '-1' if $a is less than $b, or '0' if
*/
function usort_natural(string $a, string $b) : int {
return strnatcmp($a, $b);
}
/**
* sort_by_subkey - takes the list of templates and perform a final sort
*
* @param array $array
* @param mixed $subkey
* @param int $sort
*
* @return bool - The result of the sort operation
*/
function sort_by_subkey(array &$array, mixed $subkey, int $sort = SORT_ASC) : bool {
$keys = [];
foreach ($array as $subarray) {
$keys[] = $subarray[$subkey];
}
return array_multisort($keys, $sort, $array);
}