Well the type of sorting is going to depend on the way the information is structured and how you fetched it from the database. For instance, if each email is made into an array and that array is in another array of emails, then you can use functions like asort(), rsort(), ksort(), natsort() etc.
If you fetched these items as objects from the database, stored them in a custom object that you made and put those into an array, you may want to take a look at the usort() function which will allow you to create a comparator function in which you define how the two objects will be compared and used for sorting. In this function you would most likely compare various values of fields in the object to determine which takes priority like by sorting on the email's headers or if it has more attachments than the other.
So a good start would be to check out these functions I have mentioned here in the PHP manual at PHP.net and check out the examples. Then you can go from there.
If you need help with this some more, just show us what the structure of each email looks like, how you fetch it from the database and how you store it before needing to sort. Then we can GUIDE you as to what approach might work best for you.
Good luck.