Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 找到最小值';配对';排列_Ruby On Rails_Arrays_Sorting - Fatal编程技术网

Ruby on rails 找到最小值';配对';排列

Ruby on rails 找到最小值';配对';排列,ruby-on-rails,arrays,sorting,Ruby On Rails,Arrays,Sorting,我有一个“数组对”数组: [[time, distance], [time, distance], [time, distance], [time, distance], ...] 我想找到具有最小时间的“对”的索引,如果有多个具有相同最小时间的“对”,则取具有最小距离的对 e、 g.如果我有: [[5, 5], [1,7], [2,6], [1,6]] 我想返回3。(即第四个元素的索引) 我该怎么做呢?Array#sort正是您想要的 myarray = [[5, 5], [1,7], [

我有一个“数组对”数组:

[[time, distance], [time, distance], [time, distance], [time, distance], ...]
我想找到具有最小时间的“对”的索引,如果有多个具有相同最小时间的“对”,则取具有最小距离的对

e、 g.如果我有:

[[5, 5], [1,7], [2,6], [1,6]]
我想返回3。(即第四个元素的索引)

我该怎么做呢?

Array#sort
正是您想要的

myarray = [[5, 5], [1,7], [2,6], [1,6]]
myarray.sort
#=> [[1, 6], [1, 7], [2, 6], [5, 5]]
因此,我们只需要找到原始数组中与排序数组中的第一个元素匹配的元素

ndx = myarray.index myarray.sort[0]
#=> 3

编辑:我最初使用spaceship操作符,但意识到它不是必需的。

我不熟悉RubyonRails,但我编写了一些PHP代码来实现您的目标。我希望你能从中提炼出这个想法

<?php

$timesAndDistances = [['t' => t1, 'd' => d1], ['t' => t2, 'd' => d2], ..., ['t' => tn, 'd' => dn]];

$lowestTime_withDistance = [['t' => null, 'd' => null, 'i' => null]];

foreach($timesAndDistances as $index => $timeAndDistance)
{
    $time = $timeAndDistance['t'];
    $distance = $timeAndDistance['d'];

    if($lowestTime_withDistance[0]['t'] == null)
    {
        $lowestTime_withDistance[0] = ['t' => $time, 'd' => $distance, 'i' => $index];
    }
    else 
    {
        if($lowestTime_withDistance[0]['t'] > $time){
            $lowestTime_withDistance[0] = ['t' => $time, 'd' => $distance, 'i' => $index];
        }
        elseif ($lowestTime_withDistance[0]['t'] == $time)
        {
            $lowestTime_withDistance[] = ['t' => $time, 'd' => $distance, 'i' => $index];
        }
    }
}

if(count($lowestTime_withDistance) == 1)
{
    echo 'Index of element with minimum time is ' . $lowestTime_withDistance[0]['i'];
}
elseif(count($lowestTime_withDistance) > 1){
    $lowestDistance = [['d' => null, 'i' =>null]];

    foreach($lowestTime_withDistance as $timeWithDistance)
    {
        if($lowestDistance[0]['d'] == null)
        {
            $lowestDistance[0] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
        }
        else
        {
            if($lowestDistance[0]['d'] > $timeWithDistance['d'])
            {
                $lowestDistance[0] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
            }
            elseif ($lowestDistance[0]['d'] == $timeWithDistance['d'])
            {
                $lowestDistance[] = ['d' => $timeWithDistance['d'], 'i' => $timeWithDistance['i']];
            }
        }
    }

    if(count($lowestDistance) == 1)
    {
        echo 'Index of element with minimum time and distance is ' . $lowestDistance[0]['i'];
    }
    elseif (count($lowestDistance) > 1)
    {
        foreach($lowestDistance as $aLowestDistance)
        {
            echo 'Index of an element with minimum time and distance is ' . $aLowestDistance['i'];
        }
    }
}

编写快速代码以实现此目的:

  • 假设数组a有以下元素

    [5,5],[1,7],[2,6],[1,6]]

  • 在b中创建阵列的副本:

    b=a

    b=[[5,5],[1,7],[2,6],[1,6]]

  • b类

    b=b.排序

    b=[[1,6],[1,7],[2,6],[5,5]]

  • 迭代嵌套循环

    最小值=b[0]

    索引=0

    a、 除非是零,否则每一个都是零

    如果iter_orig.eql最小

    中断

    结束

    索引++

    结束

    结束

    放入“最小时间/距离索引-”+索引