it's easier to sort it in SQL
just add ORDER BY fieldname at the end with ASC (ascending) or DESC (descending) at the end. You can have multiple order by fields also, the non-primary fields are used when the primary fields are equal
eg
SELECT * FROM mytable ORDER BY myimportantcolumn, myimportantcolumn2