Jump to content

Help with sorting an array


Jameswalker

Recommended Posts

I'm having some trouble with an array I am ordering I've tried a few sorting options, the latest is usort. Currently, it's looking at the first character of each number and ordering based on that, rather than treating the numeric characters as a full number.

I've tried adding substr but it hasn't made any difference...

The array I'm getting back at the moment looks like this:

[0] => Array
    (
        [0] => 1 The Street, The City, The County
        [1] => FriA
    )

[1] => Array
    (
        [0] => 10 The Street, The City, The County
        [1] => FriB
    )

[2] => Array
    (
        [0] => 11 The Street, The City, The County
        [1] => FriA
    )


The first value is the address, the second is a schedule. 2 The Street is currently displaying after 19, rather than 1.

Here is the code:

function cmp( $a, $b ) {
    return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) );
    if ( $a == $b ) {
        return 0;
    }
    return ( $a < $b ) ? -1 : 1;
}

foreach ( $json[ 'candidates' ] as $value ) {
    $address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ];
    $code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ];
    $a[] = array( $address, $code );
    usort($a, "cmp");
    $index++;
}

echo '<pre>';print_r( $a );echo '</pre>';

Any help would be greatly appreciated!

Link to comment
Share on other sites

Firstly

function cmp( $a, $b ) {
    return strcmp( substr( $a[ '0' ], 0, 2 ), substr( $b[ '0' ], 0, 2 ) );
    if ( $a == $b ) {
        return 0;
    }
    return ( $a < $b ) ? -1 : 1;
}

The last 4 lines of that function are redundant and never executed. All you need is the "return strcmp(...)"

 

Secondly, you should sort the array *after* you have built it. You are currently sorting inside the loop each time you add a new element.

Link to comment
Share on other sites

18 minutes ago, mac_gyver said:

 

 

Thanks so much for your help guys, as you can probably tell I'm new at this!

I've made the changes that you've suggested but now the order is randomised - I'm sure it's something I'm missing, so apologies in advance, I can tell it's pretty close...

Link to comment
Share on other sites

Just now, benanamen said:

Were is this array coming from in the first place, a database? I smell an XY problem.

Hi, it's coming from an ESRI locatorhub REST API. Here's the latest code -

function cmp($a, $b)
{
  $a = (int)$a;
  $b = (int)$b;

  return $a - $b;
}

foreach ( $json[ 'candidates' ] as $value ) {
	$address = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'ADDRESS_1' ];
	$code = $json[ 'candidates' ][ $index ][ 'attributes' ][ 'CODE' ];
	$a[] = array( $address, $code );
	$index++;
}
usort($a, 'cmp');
echo '<pre>';print_r( $a );echo '</pre>';

 

Link to comment
Share on other sites

Works for me. Have you checked your data?

$arr = [
            [ 0 => '1 The Street, The City, The County',    1 => 'FriA'   ],
            [ 0 => '21 The Street, The City, The County',   1 => 'FriA'   ],
            [ 0 => '10 The Street, The City, The County',   1 => 'FriB'   ]
        ];
usort ($arr, function($a,$b) { 
                return (int)$a[0] - (int)$b[0]; 
                });
echo '<pre>', print_r($arr, 1), '</pre>';

/***** RESULTS ******/

        Array
        (
            [0] => Array
                (
                    [0] => 1 The Street, The City, The County
                    [1] => FriA
                )

            [1] => Array
                (
                    [0] => 10 The Street, The City, The County
                    [1] => FriB
                )

            [2] => Array
                (
                    [0] => 21 The Street, The City, The County
                    [1] => FriA
                )

) 

 

Link to comment
Share on other sites

6 minutes ago, Barand said:

Works for me. Have you checked your data?


$arr = [
            [ 0 => '1 The Street, The City, The County',    1 => 'FriA'   ],
            [ 0 => '21 The Street, The City, The County',   1 => 'FriA'   ],
            [ 0 => '10 The Street, The City, The County',   1 => 'FriB'   ]
        ];
usort ($arr, function($a,$b) { 
                return (int)$a[0] - (int)$b[0]; 
                });
echo '<pre>', print_r($arr, 1), '</pre>';

/***** RESULTS ******/

        Array
        (
            [0] => Array
                (
                    [0] => 1 The Street, The City, The County
                    [1] => FriA
                )

            [1] => Array
                (
                    [0] => 10 The Street, The City, The County
                    [1] => FriB
                )

            [2] => Array
                (
                    [0] => 21 The Street, The City, The County
                    [1] => FriA
                )

) 

 

Ok, now it's working. Just tweaked the structure to match your example and it's working like a dream. Thank you so so much

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.