1

I need to sort this array by distance

Array
(
    [0] => Array
        (
            [name] => Pyro Pizza
            [distance] => 2.3
        )

    [1] => Array
        (
            [name] => Sparky's Pizza
            [distance] => 2.1
        )

    [2] => Array
        (
            [name] => American Dream Pizza - Portland
            [distance] => 0.5
        )

    [3] => Array
        (
            [name] => Ken's Artisan Pizza
            [distance] => 1.1
        )

    [4] => Array
        (
            [name] => Sparky's Pizza - SE
            [distance] => 2.2
        )

    [5] => Array
        (
            [name] => Vincente's Gourmet Pizza and the V-Room
            [distance] => 2
        )

    [6] => Array
        (
            [name] => Blind Onion Pizza and Pub
            [distance] => 0.6
        )

    [7] => Array
        (
            [name] => Hot Lips Pizza
            [distance] => 1.9
        )

    [8] => Array
        (
            [name] => Flying Pie Pizzeria
            [distance] => 2
        )

    [9] => Array
        (
            [name] => Hammy's Pizza
            [distance] => 2.4
        )

)

I used this..

usort($results, 'sortByOrder');

with this..

function sortByOrder($a, $b) {
    return $a['distance'] - $b['distance'];
}

but it doesn't work

3 Answers 3

3

This should do the job:

function sortByOrder($a, $b) {
    if ( $a ['distance'] == $b ['distance'] ) return 0;
    return ( $a ['distance'] < $b ['distance'] ) ? -1 : 1;
}

usort ( $results, 'sortByOrder' );

The problem with your code is that the comparison function, sortByOrder here, should return either 0 if they are equal or -1/1 if one of them is bigger. Your function however returned the difference in distance, which usort() can't parse.

Sign up to request clarification or add additional context in comments.

Comments

3

Your sortByOrder function doesn't look quite right.

From usort

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

4 Comments

@Kirk Strobec "Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime"
Concepts and syntax are two different worlds — In your analogy what you did is said "go get fish" as opposed to "go to the north shore with X net and Y boat at Z hour with A bait"
@Kirk Strobeck "Thanks man but I cannot figure out the function description or the several examples given on php.net - I would appreciate if you could expand some more. Cheers!"
I dont understand it either, thats why I posted the question, see the answer to learn more ;)
2
    function sortByOrder($a, $b) {
        if ($a['distance'] == $b['distance']) return 0; 
        return ($a['distance'] < $b['distance']) ? -1 : 1;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.