Jump to content

Recommended Posts

I have a multidimensional array that I want to sort by "price" then by "date".  Here is an example of the array:

$productArray   = array ( array ("Bus", "1/1/2010", 15.99),
                                        array ("Train", "2/1/2010", 14.99),
                                        array ("Car", "3/1/2010", 18.00),
                                        array ("Plane", "3/1/2010", 15.99),
                                        array ("Bike", "3/1/2010", 9.99),
                                        array ("Truck", "1/1/2010", 19.99)
);
//sort by date
function sortElement1Asc($x, $y)
{
if ( $x[1] == $y[1] )
		return 0;
        else if ( $x[1] < $y[1] )
		return -1;
        else
		return 1;
}
//sort by price
function sortElement2Asc($x, $y)
{
if ( strtotime($x[2]) == strtotime($y[2]) )
		return 0;
else if ( strtotime($x[2]) < strtotime($y[2]) )
		return -1;
else
		return 1;
}
usort($productArray, 'sortElement1Asc');
usort($productArray, 'sortElement2Asc');

 

Unfortunately this is just sorting by price (or whatever i sort last).

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/251796-multidimensional-array-multiple-sort/
Share on other sites

PFMaBiSmAd, thanks for the response.  The individual sort functions are working fine.  If I comment out usort($productArray, 'sortElement1Asc'); and run the code it sorts by price ascending and vice versa with the other usort line.  I just can't get a sort by date, sort by price to work.

 

Your two sorting functions are completely separate and have no relation to each other.

 

When you call usort($productArray, 'sortElement1Asc'); your sorting the array by the date.

 

Next, when you call usort($productArray, 'sortElement2Asc'); your sorting the array by the price, but in the process you completely overwrite the previous sorting.  This second call is a completely separate process with no relation to the previous sort.

 

What you need to do to sort by both is create one single function which will do the sorting you need.  First, compare the price.  If they differ, return -1 or 1 accordingly.  If they are equal, move on to your second condition on the date.  Compare the dates and return -1, 0, or 1 as appropriate.

 

kicken,

 

Thank you for the constructive reply.  Thanks to you I figured it out.  For anyone who is interested here is the correct (single) sort function:

 

function sortByDateThenByPrice($x, $y)
{
if ( $x[1] == $y[1] )
     if ( $x[2] == $y[2] )
          return 0;
     else if ( $x[2] < $y[2] )
          return -1;
     else
          return 1;
else if ( $x[1] < $y[1] )
     return -1;
else
     return 1;
}
usort($productArray, 'sortByDateThenByPrice');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.