diff --git a/classes/ratings.php b/classes/ratings.php index c398b885fd..61db4e7e0e 100644 --- a/classes/ratings.php +++ b/classes/ratings.php @@ -267,7 +267,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { $startsolved = $index; $starthelpful = $index; $startother = $index; - self::moodleoverflow_quicksort_posts($sortedposts, $startsolvedandhelpful, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $startsolvedandhelpful, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $startsolvedandhelpful, $index - 1); } @@ -285,7 +285,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { if ($index > $startsolved) { $starthelpful = $index; $startother = $index; - self::moodleoverflow_quicksort_posts($sortedposts, $startsolved, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $startsolved, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $startsolved, $index - 1); } @@ -299,7 +299,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { // Update the indices and sort the group by votes. if ($index > $starthelpful) { $startother = $index; - self::moodleoverflow_quicksort_posts($sortedposts, $starthelpful, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $starthelpful, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $starthelpful, $index - 1); } } else { @@ -315,7 +315,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { if ($index > $starthelpful) { $startsolved = $index; $startother = $index; - self::moodleoverflow_quicksort_posts($sortedposts, $starthelpful, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $starthelpful, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $starthelpful, $index - 1); } @@ -329,7 +329,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { // Update the indices and sort the group by votes. if ($index > $startsolved) { $startother = $index; - self::moodleoverflow_quicksort_posts($sortedposts, $startsolved, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $startsolved, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $startsolved, $index - 1); } } @@ -343,7 +343,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) { } // Update the indices and sort the group by votes. if ($index > $startother) { - self::moodleoverflow_quicksort_posts($sortedposts, $startother, $index - 1, 'votesdifference'); + moodleoverflow_quick_array_sort($sortedposts, $startother, $index - 1, 'votesdifference', 'desc'); self::moodleoverflow_check_equal_votes($sortedposts, $startother, $index - 1); } @@ -815,57 +815,6 @@ public static function moodleoverflow_user_can_rate($post, $modulecontext, $user && $post->reviewed == 1; } - /** - * Sorts answerposts of a discussion with quicksort algorithm - * @param array $posts the posts that are being sorted - * @param int $low the index from where the sorting begins - * @param int $high the index until the array is being sorted - * @param string $sortby the attribute by which the posts are being sorted, can be 'votesdifference' or 'modified' - */ - private static function moodleoverflow_quicksort_posts(array &$posts, $low, $high, $sortby): void { - if ($low >= $high) { - return; - } - $left = $low; - $right = $high; - $pivot = 0; - if ($sortby == 'votesdifference') { - $pivot = $posts[intval(($low + $high) / 2)]->votesdifference; - } else if ($sortby == 'modified') { - $pivot = $posts[intval(($low + $high) / 2)]->modified; - } - do { - if ($sortby == 'votesdifference') { - while ($posts[$left]->votesdifference > $pivot) { - $left++; - } - while ($posts[$right]->votesdifference < $pivot) { - $right--; - } - } else if ($sortby == 'modified') { - while ($posts[$left]->modified < $pivot) { - $left++; - } - while ($posts[$right]->modified > $pivot) { - $right--; - } - } - if ($left <= $right) { - $temp = $posts[$right]; - $posts[$right] = $posts[$left]; - $posts[$left] = $temp; - $right--; - $left++; - } - } while ($left <= $right); - if ($low < $right) { - self::moodleoverflow_quicksort_posts($posts, $low, $right, $sortby); - } - if ($high > $left ) { - self::moodleoverflow_quicksort_posts($posts, $left, $high, $sortby); - } - } - /** * Helper function for moodleoverflow_sort_answer_by_rating. For posts that have the same mark and votesdifference, * the posts are sorted by time modified @@ -883,7 +832,7 @@ private static function moodleoverflow_check_equal_votes(&$posts, $low, $high) { ($posts[$tempendindex]->votesdifference == $posts[$tempendindex + 1]->votesdifference)) { $tempendindex++; } - self::moodleoverflow_quicksort_posts($posts, $tempstartindex, $tempendindex, 'modified'); + moodleoverflow_quick_array_sort($posts, $tempstartindex, $tempendindex, 'modified', 'asc'); $low = $tempendindex + 1; } else { $low++; diff --git a/classes/tables/userstats_table.php b/classes/tables/userstats_table.php index 6a89d27beb..d88fa7ce21 100644 --- a/classes/tables/userstats_table.php +++ b/classes/tables/userstats_table.php @@ -398,10 +398,10 @@ private function sort_table_data($sortorder) { $length = count($this->userstatsdata); if ($sortorder['sortorder'] == 4) { // 4 means sort in ascending order. - moodleoverflow_quick_array_sort(0, $length - 1, $this->userstatsdata, $key, 'asc'); + moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'asc'); } else if ($sortorder['sortorder'] == 3) { // 3 means sort in descending order. - moodleoverflow_quick_array_sort(0, $length - 1, $this->userstatsdata, $key, 'desc'); + moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'desc'); } } } diff --git a/locallib.php b/locallib.php index fa184ff569..7454cda803 100644 --- a/locallib.php +++ b/locallib.php @@ -2136,14 +2136,15 @@ function moodleoverflow_update_all_grades() { * Function to sort an array with a quicksort algorithm. This function is a recursive function that needs to * be called from outside. * + * @param array $array The array to be sorted. It is passed by reference. * @param int $low The lowest index of the array. The first call should set it to 0. * @param int $high The highest index of the array. The first call should set it to the length of the array - 1. - * @param array $array The array to be sorted. It is passed by reference. + * * @param string $key The key/attribute after what the algorithm sorts. The key should be an comparable integer. * @param string $order The order of the sorting. It can be 'asc' or 'desc'. * @return void */ -function moodleoverflow_quick_array_sort($low, $high, &$array, $key, $order) { +function moodleoverflow_quick_array_sort(&$array, $low, $high, $key, $order) { if ($low >= $high) { return; } @@ -2175,9 +2176,9 @@ function moodleoverflow_quick_array_sort($low, $high, &$array, $key, $order) { } } while ($left <= $right); if ($low < $right) { - moodleoverflow_quick_array_sort($low, $right, $array, $key, $order); + moodleoverflow_quick_array_sort($array, $low, $right, $key, $order); } if ($high > $left) { - moodleoverflow_quick_array_sort($left, $high, $array, $key, $order); + moodleoverflow_quick_array_sort($array, $left, $high, $key, $order); } }