Skip to content

Commit

Permalink
Fix DoctrineListRepresentationFactory to sort list by requested ids (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored Sep 14, 2020
1 parent 25a15c0 commit 5e78464
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions ListRepresentation/DoctrineListRepresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ public function createDoctrineListRepresentation(
// sort the items to reflect the order of the given ids if the list was requested to include specific ids
$requestedIds = $this->listRestHelper->getIds();
if (null !== $requestedIds) {
usort($items, static function ($item1, $item2) use ($requestedIds) {
$item1Position = array_search($item1['id'], $requestedIds, true);
$item2Position = array_search($item2['id'], $requestedIds, true);
$idPositions = array_flip($requestedIds);

return $item1Position - $item2Position;
usort($items, function ($a, $b) use ($idPositions) {
return $idPositions[$a['id']] - $idPositions[$b['id']];
});
}

Expand Down

0 comments on commit 5e78464

Please sign in to comment.