Page 1 of 1

sort an array of objects

Posted: Tue Mar 09, 2004 1:05 am
by mpatters
I have EmailMsg objects which contain info such as "subject" and "sent_date".

I'm creating arrays containing all the EmailMsg objects read from the database.

I merge two arrays of 2 different sets of email message objects (yes, it's weird...but that's how it has to work for this application):

Code: Select all

$aAllMessages = array_merge($aSomeMessages,$aOtherMessages);
I can then print a list of all messages with:

Code: Select all

foreach ($aAllMessages as $msg) {
    print "Message [".$msg->subject."] was sent on ".$msg->sent_date."<br>"; 
}
How would I re-sort the "aAllMessages" array so that all the elements (EmailMsg objects) are sorted in order of their respective sent_dates, thereby making my list print out in the correctly-sorted order?

Posted: Wed Mar 10, 2004 2:06 pm
by Weirdan
take a look at [php_man]usort[/php_man]. You can supply user defined comparision function to it.