Help with sorting an array


#1

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:

[code][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
)[/code]
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:

[php]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 ‘

’;print_r( $a );echo ‘
’;[/php]

Any help would be greatly appreciated!


#2

It is currently sorting based on ASCII value. I guess you want numeric value instead? Or rather, how are you wanting this sorted and where did it come from?


#3

OP has already been answered in detail on another forum.