TYPO3 extbase query order by field values

In some cases, we need to show only the records selected in plugin flex form.

In controller, if we get those values, it will be comma separated and they are in the selected order.

But if we run the query we wont get the query output in the same order.

Using below function, we can order the output like we selected in the Flexform

function findForCategory($catId) {

  //$catId = '1,2,5,3'; example

  $catId = explode(',', $catId);

  $query = $this->createQuery();

  $query->matching( $query->in('uid', $catId) );

  $query->setOrderings($this->orderByField('uid', $catId));

  return $query->execute();



* Set the order method


protected function orderByField($field, $values) {

  $orderings = array();

  foreach ($values as $value) {

    $orderings["$field={$value}"] = \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING;


  return $orderings;