Read the MySQL manual about the ORDER BY and GROUP BY clauses.
SELECT * FROM table ORDER BY firstname asc;
this will order the results by firstname in ascending order.
Note: you can only use SQL to sort during the query, not after the query has been done. (so you can't take a resultset and re-order it before you mysql_fetch_array().