How do I sort this array?
February 25, 2007 8:52 AM   RSS feed for this thread Subscribe

How do I sort this array in PHP?

I'd like to rearrange this array, so that the first child (in this case, the person) is placed in order according to the value of one of its children (wall posts). Is this possible? I'm not too hot on PHP arrays and have been looking through the 'sort' command, but to no avail.
posted by samstarling to computers & internet (3 comments total)
array_multisort
posted by public at 9:03 AM on February 25, 2007


array_multisort() probably does the trick, but you could also use usort() which allows you to define your own callback function that will be used for sorting (see the example in the manual).
posted by jayden at 9:26 AM on February 25, 2007


I don't trust my PHP, because I haven't used the language in over a year, but here's how I'd do it in Javascript. And I know that the PHP version would be very close.

/***
* compares two objects via their wallCount properties.
* if object A's wallCount is less than objectB's wallcount,
* the function will return a -1; if the wallCounts are
* equal, it will return a zero; if B's wallCount is greater
* than A's, it will return a 1.
***/

function byWallCount(objectA,objectB)
{
if (objectA.wallCount < objectb.wallcount)br> {
return -1;
}
else if (objectA.wallCount > objectB.wallCount)
{
return 1;
}
else
{
return 0;
}
}

//use the byWallCount function to sort an entire array
facebookArray.sort(byWallCount);
posted by grumblebee at 12:24 PM on February 25, 2007


« Older Help me replace my big ugly sp...   |   Help identify this 1980s explo... Newer »
This thread is closed to new comments.