You seem to keep asking questions and its a bit uncclear exactly what you want. However keep in mind one thing ..... let sql do the work as opposed to PHP,Perl, etc. Dont pull out all the data , then sort - you want to sort , etc. then pass the relevant data into your program. You will always get better results if you do the sorting, etc. within sql. If you cant create a query for it, you will need to go back and redesign your db (write it down first , dont create as you go). If you wing it you will run into issues down the road , be they performance, normalization, etc.
Rory
