Page 1 of 1

Array Sorting Madness

Posted: Mon Aug 04, 2008 6:35 am
by shiznatix
Here is the situation. I have a multidimensional array that I am trying to sort by 2 values. Let me example your face:

Code: Select all

 
Array
(
    [0] => Array
        (
            [username] => guy1
            [paymentMethod] => Paypal
            [totalPayment] => 10.33
        )
 
    [1] => Array
        (
            [username] => guy2
            [paymentMethod] => McDonalds
            [totalPayment] => 8.41
        )
 
    [2] => Array
        (
            [username] => guy3
            [paymentMethod] => Paypal
            [totalPayment] => 1.00
        )
 
    [3] => Array
        (
            [username] => guy4
            [paymentMethod] => Pizza Hut
            [totalPayment] => 75.1
        )
 
    [4] => Array
        (
            [username] => guy5
            [paymentMethod] => McDonalds
            [totalPayment] => 50.22
        )
 
)
 
So I have that array. What I want to do is first group it by the 'paymentMethod'. So after the first sort it would look like this:

Code: Select all

 
Array
(
    [0] => Array
        (
            [username] => guy2
            [paymentMethod] => McDonalds
            [totalPayment] => 8.41
        )
 
    [1] => Array
        (
            [username] => guy5
            [paymentMethod] => McDonalds
            [totalPayment] => 50.22
        )
 
    [2] => Array
        (
            [username] => guy1
            [paymentMethod] => Paypal
            [totalPayment] => 10.33
        )
 
    [3] => Array
        (
            [username] => guy3
            [paymentMethod] => Paypal
            [totalPayment] => 1.00
        )
 
    [4] => Array
        (
            [username] => guy4
            [paymentMethod] => Pizza Hut
            [totalPayment] => 75.1
        )
 
)
 
So all the payment methods are together. Next I want to order those payment methods by the totalPayment from higest to lowest but still retaining the paymentMethod grouping. So in the end, I want the original array to look like this:

Code: Select all

 
Array
(
    [1] => Array
        (
            [username] => guy5
            [paymentMethod] => McDonalds
            [totalPayment] => 50.22
        )
 
    [0] => Array
        (
            [username] => guy2
            [paymentMethod] => McDonalds
            [totalPayment] => 8.41
        )
 
    [2] => Array
        (
            [username] => guy1
            [paymentMethod] => Paypal
            [totalPayment] => 10.33
        )
 
    [3] => Array
        (
            [username] => guy3
            [paymentMethod] => Paypal
            [totalPayment] => 1.00
        )
 
    [4] => Array
        (
            [username] => guy4
            [paymentMethod] => Pizza Hut
            [totalPayment] => 75.1
        )
 
)
 
and that would be that. The only way I can think of doing this now is to break up the whole array into smaller arrays, 1 for each paymentMethod, then using a nice little usort() call on each array then building them all back together but this just seams wasteful to me. So, I ask you guys, do you know a simpler way to do this? Some angle I haven't thought about maybe?

Re: Array Sorting Madness

Posted: Mon Aug 04, 2008 7:08 am
by VladSun
Untested:

Code: Select all

function cmp($a, $b)
{
    if ($a['paymentMethod'] == $b['paymentMethod']) {
        if ($a['totalPayment'] == $b['totalPayment']) {
              return 0;
        }
        return ($a['totalPayment'] < $b['totalPayment']) ? -1 : 1;
    }
    return ($a['paymentMethod'] < $b['paymentMethod']) ? -1 : 1;
}
 
usort($array, "cmp");
 

Re: Array Sorting Madness

Posted: Mon Aug 04, 2008 7:36 am
by shiznatix
Ha super thanks. That works perfectly. I think i need to get more sleep, I can't function anymore.