Page 1 of 1

Sorting Help

Posted: Mon Jan 14, 2008 6:43 am
by shiznatix
I have an array that looks like this:

Code: Select all

[0] => Array
                (
                    [rowClass] => table-row-one
                    [username] => ********
                    [grossRake] => 344.60
                    [deductions] => 0
                    [deductionsStyle] => color: black;
                    [netRake] => $344.60
                    [rakeback] => $130.95
                    [rakebackPercent] => 38%
                )
 
            [1] => Array
                (
                    [rowClass] => table-row-two
                    [username] => ********
                    [grossRake] => 56.77
                    [deductions] => 0
                    [deductionsStyle] => color: black;
                    [netRake] => $56.77
                    [rakeback] => $21.57
                    [rakebackPercent] => 38%
                )
 
            [2] => Array
                (
                    [rowClass] => table-row-one
                    [username] => ********
                    [grossRake] => 2.00
                    [deductions] => 0
                    [deductionsStyle] => color: black;
                    [netRake] => $2.00
                    [rakeback] => $0.76
                    [rakebackPercent] => 38%
                )
 
            [3] => Array
                (
                    [rowClass] => table-row-two
                    [username] => ********
                    [grossRake] => 72.84
                    [deductions] => 0
                    [deductionsStyle] => color: black;
                    [netRake] => $72.84
                    [rakeback] => $27.68
                    [rakebackPercent] => 38%
                )
and I am looking to sort this by the grossRake DESC. Is there some internal php function that I can use to sort it by that or should I start pumping out some custom sorting code for that? Sorry if this is a easy to answer question, im super jetlagged and thus very tired.

Re: Sorting Help

Posted: Mon Jan 14, 2008 6:49 am
by Kieran Huggins
check out array_multisort(), but you should really be doing your sorting in the DB - much faster.

Re: Sorting Help

Posted: Mon Jan 14, 2008 7:03 am
by jimthunderbird
Maybe you would want to take a look at http://us.php.net/manual/en/function.usort.php#67936

Re: Sorting Help

Posted: Mon Jan 14, 2008 10:57 am
by pickle
+1 to Keiran's reply

Re: Sorting Help

Posted: Mon Jan 14, 2008 1:16 pm
by jimthunderbird
pickle wrote:+1 to Keiran's reply
Yes, I will do the same to Keiran, his solution is much better. And if sorted in DB would be much easier.

Re: Sorting Help

Posted: Tue Jan 15, 2008 1:47 am
by Kieran Huggins
woohoo! so that's +2 in all? Not that I'm counting or anything...

That invisible pink unicorn will soon be mine ;-)

Re: Sorting Help

Posted: Tue Jan 15, 2008 5:28 am
by shiznatix
Ok everyone says array_multisort() is the key but I don't see how to actually do what I want with it. I have tried a bunch of stuff but have gotten nowhere.

Also, I can't do it in the DB because of calculations that I doing on the returned data in PHP can't be done in SQL as far as I know.

Re: Sorting Help

Posted: Tue Jan 15, 2008 6:29 am
by VladSun
Look at Example#3 Sorting database results http://bg.php.net/array_multisort ... :)
I would suggest you to use http://bg.php.net/manual/en/function.usort.php instead - I think it will be faster than array_multisort, because you have to rotate the array first.

What calculations do you need that you cant do at DB layer?

Re: Sorting Help

Posted: Tue Jan 15, 2008 6:51 am
by shiznatix
I can't do it in the DB because after I get a list of usernames, I then get their data (what ends up returning the grossRake stuff) and I have getting that data in another function. I want to keep that function separate because its complex and subject to change at any minute and I call it from many parts of the site so if I had to change 1 thing in the function, I would have to change it in every single place on the site which would be really tough and just bad design. So I take a small preformance hit (its small) for a well designed site.

I am trying to use usort, as it seams like it would be the best way since I don't understand array_multisort() (yes, I read the silly manual). I have this code:

Code: Select all

 
//sort the array based on the gross rake
function grossRakeSort($a, $b)
{
    if ($a['grossRake'] == $b['grossRake'])
    {
        return 0;
    }
    
    return ($a['grossRake'] < $b['grossRake']) ? -1 : 1;
}
 
usort($usermapContent, 'grossRakeSort');
//end sorting the array
 
but that does not work :( and I don't see why. Its just a simple cut and past from the site with my vars thrown in but it logically to me seams like it should work.

Re: Sorting Help

Posted: Tue Jan 15, 2008 7:00 am
by VladSun
I've copied/pasted *your* code and it works ...
http://ipclassify.relef.net/2.php
http://ipclassify.relef.net/2.phps

Re: Sorting Help

Posted: Tue Jan 15, 2008 7:48 am
by shiznatix
Zomg, I was being very stupid. I was putting it in the sort with the $ in the grossRake so it was comparing strings, not numbers! Arg. Hours have been spent on this. Oh well.

Re: Sorting Help

Posted: Tue Jan 15, 2008 7:57 am
by VladSun
In your first example data grossRake is a numeric type, and in your second example data it is a string.

Code: Select all

 
 function grossRakeSort($a, $b)
 {
     if (floatval(substr($a['grossRake'], 1)) == floatval(substr($b['grossRake'], 1)))
         return 0;
    
     return (floatval(substr($a['grossRake'], 1)) < floatval(substr($b['grossRake'], 1))) ? -1 : 1;
 }
 

Re: Sorting Help

Posted: Tue Jan 15, 2008 7:58 am
by VladSun
shiznatix wrote:Zomg, I was being very stupid. I was putting it in the sort with the $ in the grossRake so it was comparing strings, not numbers! Arg. Hours have been spent on this. Oh well.
:) Always give the real data when asking for help ;)