public function sort($results)
{
$isCollection = $results instanceof LdapObjectCollection;
$results = $isCollection ? $results->toArray() : $results;
// We need to track the index when sorting to ensure a stable sort. This is essentially how PHP 7+ handles
// things, but not PHP 5.6. I think this would be the expected order, especially for tests/specs...
foreach ($results as $index => &$item) {
$item = [$index + 1, $item];
}
usort($results, [$this, 'resultSortCallback']);
// Sort done, so remove the index tracking from the results...
foreach ($results as &$item) {
$item = $item[1];
}
return $isCollection ? new LdapObjectCollection(...$results) : $results;
}