1; --$i) { $m = 0; for ($j = 0; $j < $i; ++$j) { if ($list[$j]->compare($list[$m], $order)) { $m = $j; } } if ($m !== $i - 1) { // flip max/min to the beginning $start = 0; $c = $m; while ($start < $c) { $temp = $list[$start]; $list[$start] = $list[$c]; $list[$c] = $temp; ++$start; --$c; } // flip reverse array $start = 0; $c = $i - 1; while ($start < $c) { $temp = $list[$start]; $list[$start] = $list[$c]; $list[$c] = $temp; ++$start; --$c; } } } return $list; } }