Apply possible answers order to query
@param ActiveQuery $query
@param $order
@return string
961837
public static function applyOrder(ActiveQuery $query, $order)
    {
        switch ($order) {
            case 'oldest':
                $query->orderBy('created_at DESC');
                break;

            case 'active':
                $query->orderBy('created_at ASC');
                break;

            case 'votes':
            default:
                $query->orderBy('votes DESC');
                break;
        }

        return $order;
    }
150640
public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC')
    {
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
        $today = new \DateTime("now");
        $today = $today->format('Y-m-d H:i:s');

        $onlineGames = '(
            (
                CASE WHEN (
                    g.active = 1
                    AND g.broadcastPlatform = 1
                    AND (g.startDate <= :date OR g.startDate IS NULL)
                    AND (g.closeDate >= :date OR g.closeDate IS NULL)
                ) THEN 1 ELSE 0 END
            ) +
            (
                CASE WHEN (
                    g.active = 0
                    AND (g.broadcastPlatform = 0 OR g.broadcastPlatform IS NULL)
                    AND g.startDate > :date
                    AND g.closeDate < :date
                ) THEN 1 ELSE 0 END
            )
        )';

        $qb = $em->createQueryBuilder();
        $qb->select('g')->from('PlaygroundGame\Entity\Game', 'g');
        
        switch ($type) {
            case 'startDate':
                $qb->orderBy('g.startDate', $order);
                break;
            case 'activeGames':
                $qb->orderBy('g.active', $order);
                break;
            case 'onlineGames':
                $qb->orderBy($onlineGames, $order);
                $qb->setParameter('date', $today);
                break;
            case 'createdAt':
                $qb->orderBy('g.createdAt', $order);
                break;
        }

        $query = $qb->getQuery();

        return $query;
    }
1277557
public function apply(Query $query) : Query
    {
        $method = is_array($this->value) ? 'in' : 'eq';

        return $query->where(
            (new QueryExpression())->{$method}(
                $this->field,
                [] === $this->value ? '' : $this->value,
                $query->getTypeMap()->type($this->field)
            )
        );
    }
1267691
public function sort_columns($query)
    {
        // Bail if we are not on the correct admin page.
        if (! $query->is_main_query() || ! is_admin() || $query->get('post_type') !== $this->get_name()) {
            return;
        }

        $order_by = $query->get('orderby');

        // Check if the current sorted column has a sort callback defined.
        if (isset($this->sortable_columns[ $order_by ])) {
            $callback = "sort_{$order_by}_column";
            $this->{$callback}($query);
        }
    }
902978
public static function getActiveCategories($order = [])
    {
        return self::find()
            ->select([
                'category_id',
                'slug',
                'image',
                'meta_title',
                'meta_description',
                'title',
                'h1',
                'description',
                'seotext',
                'param1',
                'param2',
                'param3',
                'videos_num',
                'on_index',
            ])
            ->where(['enabled' => 1])
            ->orderBy($order)
            ->asArray()
            ->all();
    }
750960
public function apply($query)
    {
        $this->forgetJoins();

        $this->applyParameters($query);

        $this->applyJoins($query);

        return $query;
    }
219702
protected function compileOrdersToArray(Builder $query, $orders)
    {
        return array_map(function ($order) {
            return $order['sql'] ?? $this->wrap($order['column']).' '.$order['direction'];
        }, $orders);
    }
551352
public function getAllSortByQuery($sort, $order = 'DESC')
    {
        $qb = $this->createQueryBuilder('p');
        $qb2 = $this->getEntityManager()->getRepository(PageSnapshot::class)->createQueryBuilder('pp');
        $qb->select('p', 'ps')
            ->leftJoin('p.snapshots', 'ps')
            ->where($qb->expr()->eq(
                'ps.id',
                '('.$qb2->select('MAX(pp.id)')
                    ->where('p.id = pp.page')
                    ->getDQL().')'
            ))
            ->orWhere('ps.id IS NULL')
            ->orderBy('p.'.$sort,  $order);

        return $qb->getQuery();
    }
1006919
private function getSearchQueryOrderBy($query, $orderBy)
	{
		foreach ($orderBy as $key => $value) {
			$query->orderBy($key, $value);
		}

		return $query;
	}
315761
def order(self, order):
    '''

    '''
    order = order if isinstance(order, Order) else Order(order)

    # ensure order gets attr values the same way the rest of the query does.
    order.object_getattr = self.object_getattr
    self.orders.append(order)
    return self
948843
protected function handleOrderings(array $ordering)
    {
        if (empty($ordering)) {
            return '';
        }

        $sql = [];

        foreach ($ordering as $order) {
            if ($order['nulls'] !== null) {
                $sql[] = $this->columns($order['columns']) . ' ' . $order['order'] . ' ' . $order['nulls'];
            } else {
                $sql[] = $this->columns($order['columns']) . ' ' . $order['order'];
            }
        }

        return ' ORDER BY ' . implode(', ', $sql);
    }
480293
protected function setOrder(Select $query, array $order)
    {
        // Normalize to [['column', 'direction']] format if only one column
        if (! is_array(Arr::get($order, 0))) {
            $order = [$order];
        }

        foreach ($order as $key => $orderValue) {
            if (is_array($orderValue)) {
                // Column and direction
                $query->order(
                    Arr::get($orderValue, 0).' '.Arr::get($orderValue, 1)
                );
            }
        }

        return $query;
    }
47144
protected function getOrderQuery( $orders = array() )
	{
		if( is_array( $orders ) && count( $orders ) )
		{
			$order = array();
			foreach( $orders as $key => $value )
				$order[] = $key.' '.$value;
			$orders = ' ORDER BY '.implode( ', ', $order );
		}
		else
			$orders = '';
		return $orders;
	}
643927
protected function checkOrder(Sql\Select $query)
    {
        $order  = $this->getEntityDescriptor()->getPrimaryKey()->getField();
        $order .= " DESC";
        if (null != $this->order) {
            $order = $this->order;
        }
        $query->order($order);
        return $this;
    }
866858
public function setOrderBy($order)
	{
		if(is_null($order))
		{
			$this->getSelect()->setOrder(null);
			return $this;
		}

		if(!is_array($order))
		{
			$order = array($order);
		}

		$sort = array();
		foreach($order as $value)
		{
			$direction = 1;
			if (preg_match('/(.*\W)(ASC|DESC)\b/si', $value, $matches)) {
				$value = trim($matches[1]);
				switch($matches[2])
				{
					case "ASC":
						$direction = 1;
						break;
					case "DESC":
						$direction = -1;
						break;
				}
			}

			$sort[$value] = $direction;
		}

		$this->getSelect()->setOrder($sort);
		return $this;
	}
81602
final protected static function validateOrder (string $order="DESC") : string
	{
		$order = strtoupper($order);
		return (!in_array($order, ["DESC", "ASC"])) ? "DESC" : $order;
	}
878674
private function addOrders(ModelCriteria &$query)
    {
        Logger::log(static::class . ' extract orders start ', LOG_DEBUG);
        $orderAdded = FALSE;
        $tableMap = $this->getTableMap();
        foreach ($this->order->getOrders() as $field => $direction) {
            if ($column = ApiHelper::checkFieldExists($tableMap, $field)) {
                $orderAdded = TRUE;
                if ($direction === Order::ASC) {
                    $query->addAscendingOrderByColumn($column->getPhpName());
                } else {
                    $query->addDescendingOrderByColumn($column->getPhpName());
                }
            }
        }
        if (!$orderAdded) {
            $pks = $this->getPkDbName();
            foreach(array_keys($pks) as $key) {
                $query->addAscendingOrderByColumn($key);
            }
        }
        Logger::log(static::class . ' extract orders end', LOG_DEBUG);
    }