Sorting Help

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
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Sorting Help

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Sorting Help

Post by Kieran Huggins »

check out array_multisort(), but you should really be doing your sorting in the DB - much faster.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Sorting Help

Post by jimthunderbird »

Maybe you would want to take a look at http://us.php.net/manual/en/function.usort.php#67936
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Sorting Help

Post by pickle »

+1 to Keiran's reply
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Sorting Help

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Sorting Help

Post 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 ;-)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Sorting Help

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting Help

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Sorting Help

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting Help

Post by VladSun »

I've copied/pasted *your* code and it works ...
http://ipclassify.relef.net/2.php
http://ipclassify.relef.net/2.phps
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Sorting Help

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting Help

Post 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;
 }
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting Help

Post 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 ;)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply