Page 1 of 1

usort help with sorting numbers

Posted: Thu May 20, 2010 8:16 pm
by Mr Tech
I have an array that I want to sort by the "weight" key from biggest to smallest. Here is my code that almost works:

Code: Select all

function cmp_items_array($a, $b) {
    return strcmp($b['weight'], $a['weight']);
}
usort($items_array, "cmp_items_array");
Bu what it does is savy I have the number 7.29 and a number 20, it puts 7.29 (as though it's really 72.9) as greater than 20 instead of the other way around. Here is a dump of my array so you can see the results after I run the above code... 7.29 should be at the bottom of the array...

Code: Select all

Array
(
    [0] => Array
        (
            [weight] => 7.29
        )

    [1] => Array
        (
            [weight] => 20
        )

    [2] => Array
        (
            [weight] => 20
        )

    [3] => Array
        (
            [weight] => 20
        )

    [4] => Array
        (
            [weight] => 20
        )

    [5] => Array
        (
            [weight] => 10
        )

    [6] => Array
        (
            [weight] => 10
        )

)

Re: usort help with sorting numbers

Posted: Thu May 20, 2010 8:21 pm
by Chalks

Code: Select all

function cmp_items_array($a, $b) {
    return (floatval($b['weight']) < floatval($a['weight']));
}
usort($items_array, "cmp_items_array");
There you go.
strcmp compares the characters. you want to compare the values of the string.

Re: usort help with sorting numbers

Posted: Thu May 20, 2010 8:28 pm
by Mr Tech
Thanks Chalks!

That code seems to do something weird... It seems to join one of the 10 weights with the 7... See my dump:

Code: Select all

Array
(
    [0] => Array
        (
            [weight] => 17.29
        )

    [1] => Array
        (
            [weight] => 10
        )

    [2] => Array
        (
            [weight] => 20
        )

    [3] => Array
        (
            [weight] => 20
        )

    [4] => Array
        (
            [weight] => 20
        )

    [5] => Array
        (
            [weight] => 20
        )

)
Here is what it was with my original code:

Code: Select all

Array
(
    [0] => Array
        (
            [weight] => 7.29
        )

    [1] => Array
        (
            [weight] => 20
        )

    [2] => Array
        (
            [weight] => 20
        )

    [3] => Array
        (
            [weight] => 20
        )

    [4] => Array
        (
            [weight] => 20
        )

    [5] => Array
        (
            [weight] => 10
        )

    [6] => Array
        (
            [weight] => 10
        )

)

Re: usort help with sorting numbers

Posted: Thu May 20, 2010 8:37 pm
by Chalks
Mr Tech wrote:That code seems to do something weird... It seems to join one of the 10 weights with the 7... See my dump:
Uh... that doesn't really make sense. floatval() won't change the value inside the string. Can you "print_r($items_array)"?

Notice, it did put the information in the (almost) right order though... just change the '<' to '>'.

Re: usort help with sorting numbers

Posted: Thu May 20, 2010 8:46 pm
by Mr Tech
Gah! Sorry my bad. Your code worked :) Sorry for the conufsion :crazy: