Why not use order by clause in your query itself ?
$query = "select * from table order by colname [asc | desc ]";
[asc | desc ] is optional. You can sort the records in ascending order or descending order on one or multiple columns.
Or, you can also fetch the records from the table into an array and sort the array and get the max or min value from it.