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).

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

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作