Search code examples
phparrayssortingmultidimensional-arraycustom-sort

Sort rows of a 2d array by address column prioritizing street name, then odd street numbers before evens, then street numbers


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

)

Solution

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