From 5e78464a4c7326408de0865396bd394df1f8b296 Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Mon, 14 Sep 2020 11:25:33 +0200 Subject: [PATCH] Fix DoctrineListRepresentationFactory to sort list by requested ids (#8) --- ListRepresentation/DoctrineListRepresentationFactory.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ListRepresentation/DoctrineListRepresentationFactory.php b/ListRepresentation/DoctrineListRepresentationFactory.php index 55dd3c7..56afaec 100644 --- a/ListRepresentation/DoctrineListRepresentationFactory.php +++ b/ListRepresentation/DoctrineListRepresentationFactory.php @@ -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']]; }); }