doulao1934 2014-05-16 06:03
浏览 58
已采纳

按价值排序多维数组 - 然后赔率平均

I'm working with multi-dimensional arrays. Each array consists of properties along a street and one of the values is street address.

I'd like to order the arrays such that for each street, the odd addresses are listed before the even addresses. It's already ordered numerically (lowest to highest), so the only thing I'm trying to figure out is how to order the odds before the evens.

$array = array(
    array('apn' => 345345345, 'sqft' => 1200, 'address' => '323 Pacific Ave.'),
    array('apn' => 345345342, 'sqft' => 1421, 'address' => '324 Pacific Ave.'),
    array('apn' => 345345346, 'sqft' => 1012, 'address' => '325 Pacific Ave.'),
    array('apn' => 345345347, 'sqft' => 1093, 'address' => '328 Pacific Ave.'),
    array('apn' => 345345353, 'sqft' => 1121, 'address' => '12 Lincoln Ave.'),
    array('apn' => 345345351, 'sqft' => 1643, 'address' => '13 Lincoln Ave.'),
    array('apn' => 345345352, 'sqft' => 1222, 'address' => '14 Lincoln Ave.')
);

Currently I have the following:

usort($array, function($a, $b)
{
    if ($a['address'] % 2 == $b['address'] % 2) {
        if ($a['address'] == $b['address']) {
            return 0;
        }
        return ($a['address'] < $b['address']) ? -1 : 1;
    }    
    return ($a['address'] % 2 == 0) ? 1 : -1;
});

The problem is that it results in all the odds listed before all the evens, rather than all the odds of each street before the eves for that street, as follows:

Array
(
    [0] => Array
        (
            [apn] => 345345341
            [sqft] => 1001
            [address] => 13 Lincoln Ave.
        )
    [1] => Array
        (
            [apn] => 345345341
            [sqft] => 1001
            [address] => 12 Lincoln Ave.
        )

    [2] => Array
        (
            [apn] => 345345341
            [sqft] => 1001
            [address] => 14 Lincoln Ave.
        )

    [3] => Array
        (
            [apn] => 345345345
            [sqft] => 1200
            [address] => 323 Pacific Ave.
        )

    [4] => Array
        (
            [apn] => 345345341
            [sqft] => 1001
            [address] => 325 Pacific Ave.
        )

    [5] => Array
        (
            [apn] => 345345342
            [sqft] => 1421
            [address] => 324 Pacific Ave.
        )

    [6] => Array
        (
            [apn] => 345345341
            [sqft] => 1001
            [address] => 328 Pacific Ave.
        )

)
  • 写回答

1条回答 默认 最新

  • duanji4870 2014-05-16 06:27
    关注

    So that will be:

    usort($array, function($x, $y)
    {
       $x = preg_split('/^(\d+)\s+(.*)/', $x['address'], -1 , 3);
       $y = preg_split('/^(\d+)\s+(.*)/', $y['address'], -1 , 3);
       $x[2] = $x[0] % 2;
       $y[2] = $y[0] % 2;
       if($x[1]==$y[1]) //first level: streets
       {
          if($x[2]==$y[2]) //second level: oddity
             return $x[0]<$y[0]?-1:$x[0]!=$y[0]; //third level: numeric order
          return $x[2]>$y[2]?-1:1;
       }
       return $x[1]<$y[1]?-1:1;
    });
    

    Note, your difficulty is because you have, actually, 3 levels of sorting:

    • First level: streets. You want streets will be top-level of sorting in resulting array
    • Second level: oddity. You want odd numbers will be before even
    • Third level: natural numeric order - inside same oddity, you want numbers will be sorted in natural order

    Fiddle is available here. Also, preg_split() may be not the best solution to separate number and street, but I wasn't sure about common situation. A little explanation for there, -1 means "no limit for elements in result" and 3 means PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY (check the link I've provided for more details).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器