can't this be done with sql? (look up the "distinct" keyword)
here's a snippet which demonstrates the solution with php:
$data = [your database query - this should be an array of fetched records]
$newdata = array();
foreach($data as $record)
{
if(!in_array($record, $newdata))
$newdata[] = $record;
}
$data = $newdata;
collect every record from the query in a new array - ONLY if it is not there yet.
and for the sort, if you get your database query results in an array, you can easily use the sort functions for arrays which are in the php manual: sort, ksort, array_multisort etc