diff options
-rw-r--r-- | lib/Downloads.php | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/lib/Downloads.php b/lib/Downloads.php index c31381d59..cdb5501c6 100644 --- a/lib/Downloads.php +++ b/lib/Downloads.php @@ -88,8 +88,8 @@ class Downloads /** * Sort 2D array by multiple associative or numeric keys. * $sorted_array = self::sort_2d_array_by_multiple_keys($unsorted_array, 'first key', 'second', ...); - * - * based on SortArray http://php.net/manual/en/function.usort.php#42535 + * + * Note that keys are preserved! * * @param array $unsorted_array * @@ -103,21 +103,28 @@ class Downloads */ public static function sort_2d_array_by_multiple_keys() { - $arguments = func_get_args(); - $array = $arguments[0]; - $anonymous_function = ''; - $num_of_arguments = count($arguments); - for ($cur_argument = 1; $cur_argument < $num_of_arguments; $cur_argument++) { - $anonymous_function .= "if (\$first['$arguments[$cur_argument]'] != \$second['$arguments[$cur_argument]']) {"; - $anonymous_function .= " \$compare_result = strcoll(\$first['$arguments[$cur_argument]'], \$second['$arguments[$cur_argument]']);"; - $anonymous_function .= " if (0 == \$compare_result) { return 0; };"; - $anonymous_function .= " return ((0 > \$compare_result) ? -1 : 1);"; - $anonymous_function .= "}"; - } - $anonymous_function .= "return 0;"; - $compare_function = create_function("\$first, \$second", $anonymous_function); - usort($array, $compare_function); - return $array; + $arguments = func_get_args(); + $unsorted_array = array_shift($arguments); + $sort_order = $arguments; + uasort($unsorted_array, function($first_value, $second_value) use($sort_order) { + $result = 0; + for($argument_num = 1, $num_of_arguments = count($sort_order); $argument_num <= $num_of_arguments; $argument_num += 1) { + $key = $sort_order[$argument_num - 1]; + //~ $sorting = $sort_order[$argument_num]; + $first_compare_value = $first_value[$key]; + $second_compare_value = $second_value[$key]; + $comparison = strcoll($first_compare_value, $second_compare_value); + if(0 == $comparison) { + $temp_result = 0; + } else { + $temp_result = (0 > $comparison) ? -1 : 1; + } + $result += $temp_result; + $result = 10 * $result; + } + return $result; + }); + return $unsorted_array; } /** @@ -237,17 +244,18 @@ class Downloads } if (false === @file_get_contents($test_file)) { $num_dn++; - echo "Down $num_dn (up: $num_up, about $mirrs_processed mirrors tested) $test_file \n"; + echo "Down $num_dn (up: $num_up, about $mirrs_processed of _all_ mirrors tested) $test_file \n"; } else { $num_up++; - echo "Up $num_up (down: $num_dn, about $mirrs_processed mirrors tested) $test_file \n"; + echo "Up $num_up (down: $num_dn, about $mirrs_processed of _all_ mirrors tested) $test_file \n"; $mirrors['_C:' . $mirr_continent][] = $item; } } } ksort($mirrors); foreach ($mirrors as &$continent) { - $continent = self::sort_2d_array_by_multiple_keys($continent, 'zone', 'country', 'city', 'url'); + $sorted_continent = self::sort_2d_array_by_multiple_keys($continent, 'zone', 'country', 'city', 'url'); + $continent = array_values($sorted_continent); } unset($continent); |