usort help with sorting numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

usort help with sorting numbers

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

)
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: usort help with sorting numbers

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Re: usort help with sorting numbers

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

)
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: usort help with sorting numbers

Post 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 '>'.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Re: usort help with sorting numbers

Post by Mr Tech »

Gah! Sorry my bad. Your code worked :) Sorry for the conufsion :crazy:
Post Reply