在不使用php内置函数的情况下将每5个数字数组的排序从升序更改为降序,反之亦然

Posted

技术标签:

【中文标题】在不使用php内置函数的情况下将每5个数字数组的排序从升序更改为降序,反之亦然【英文标题】:Change the sorting of every 5 array of number from ascending to descending and vice versa without using php built in function [closed] 【发布时间】:2022-01-09 17:41:51 【问题描述】:

我下面有一个数组号,

$arr = [2,5,1,12,-5,4,-1,3,-3,20,8,7,-2,6,9]

预期的输出必须是:

-5,-3,-2,-1,1,20,12,9,8,7,2,3,4,5,6

排序必须每5个数组从升序变为降序,反之亦然。因此,如果前 5 位按升序排序,则后 5 位必须按降序排序。

但我得到了:

-5,-3,-2,-1,1,2,3,4,5,6,7,8,9,12,20

在我的代码下面:

$arr = array(2,5,1,12,-5,4,-1,3,-3,20,8,7,-2,6,9);

function arr_sort($array) 
    // get the size of array
    $countArr = count($array);
    
    for ($i=0; $i<$countArr; $i++) 
        for ($j=$i; $j<$countArr; $j++) 
            if ($array[$i] > $array[$j]) 
                $temporary = $array[$i];
                $array[$i] = $array[$j];
                $array[$j] = $temporary;
            
        
    
    
    // order ascending and descending
    $array = implode(',', $array);
    return $array;


print_r(arr_sort($arr));

【问题讨论】:

我还是不明白你是怎么得到输出的,分组是怎么工作的?你只是把第一个数组分成几组然后排序还是先排序然后再分成几组再使用? 我想我会创建两个数组,一个向上排序,一个向下排序,然后每次取 5 个。 我的意思是第一次排序是升序,但是排序后的数组中每5个数的倍数,排序方式由升序变为降序,每5个数的倍数再变化一次。 所以你想先对输入的正常数组进行排序,然后然后将这些排序的数字分成五个一组?然后每隔一秒反转这些组的顺序? 我看了三遍,包括cmets,我也不明白如何得到想要的输出。 【参考方案1】:

使用 php 内置函数肯定有帮助:

/**
 * @param int[] $numbers
 * @return int[]
 */
function arr_sort(array $numbers): array

    // Sort items in ascending order first
    sort($numbers, SORT_NUMERIC);

    $result = [];
    $fetch_lowest = true;

    // Process until the input array is empty
    while (count($numbers) !== 0) 
        if ($fetch_lowest) 
            // Extract the 5 lowest numbers and keep them into ascending order
            $extract = array_splice($numbers, 0, 5);
         else 
            // Extract the 5 highest numbers and reverse items to descending order
            $extract = array_splice($numbers, -5, 5);
            $extract = array_reverse($extract);
        

        // Save the extracted items and switch the highest/lowest flag
        $result = array_merge($result, $extract);
        $fetch_lowest = !$fetch_lowest;
    

    return $result;


$input = [2,5,1,12,-5,4,-1,3,-3,20,8,7,-2,6,9];
$sorted = arr_sort($input);

var_dump(implode(',', $sorted));

string(35) "-5,-3,-2,-1,1,20,12,9,8,7,2,3,4,5,6"

【讨论】:

太棒了。但是我怎样才能删除 string(35) 呢? @Sa'adAbdurrazzaq 这只是 var_dump() 输出,用于演示目的。对函数的结果做任何你想做的事情,它是一个按你想要排序的数组。 要删除字符串(35),我使用print_r @Sa'adAbdurrazzaq print_r() & var_dump() 只是调试函数。您不会在生产环境中使用它们。【参考方案2】:

我认为这就是你想要的。我们将数组分成 5 个一组,然后一次分组一个,首先从前面,然后从后面,重复该模式。如果我们从后面取,那么我们也需要反向排序。


$arr = [2,5,1,12,-5,4,-1,3,-3,20,8,7,-2,6,9];
$a = $arr;
asort($a);

$a = array_chunk($a, 5);

$output = [];
$idx = 0;
while($a)
    if($idx++ % 2)
        $b = array_pop($a);
        arsort($b);
    else
        $b = array_shift($a);
    
    $output = array_merge($output, $b);


var_dump($output);

在这里演示:https://3v4l.org/KGgWI

【讨论】:

以上是关于在不使用php内置函数的情况下将每5个数字数组的排序从升序更改为降序,反之亦然的主要内容,如果未能解决你的问题,请参考以下文章