Apply ordering.

@param $element
@param $direction

@throws \Exception

@return $this
482727
public function sort($element, $direction)
    {
        // simple sorting
        if (in_array($element, $sortable = app('scaffold.module')->sortable(), true)) {
            if ($table = app('scaffold.module')->model()->getTable()) {
                $element = "{$table}.{$element}";
            }

            $this->query->orderBy($element, $direction);
        }

        if (array_key_exists($element, $sortable) && is_callable($callback = $sortable[$element])) {
            $this->query = call_user_func_array($callback, [$this->query, $element, $direction]);
        }

        if (array_key_exists($element, $sortable) && is_string($handler = $sortable[$element])) {
            $handler = new $handler($this->query, $element, $direction);
            if (!$handler instanceof QueryBuilder || !method_exists($handler, 'build')) {
                throw new \Exception('Handler class must implement '.QueryBuilder::class.' contract');
            }
            $this->query = $handler->build();
        }

        return $this;
    }
1205950
public function orderBy(string $key, string $direction = 'ASC'): SelectQueryBuilder
    {
        array_push($this->orderBy, [
            'key' => $key,
            'direction' => strtoupper($direction),
        ]);
        return $this;
    }
914130
private Sort withDirection(final Direction direction) {

    return Sort.by(orders.stream().map(it -> new Order(direction, it.getProperty()))
        .collect(Collectors.toList()));
  }
714494
public function replaceWith(Element $element) 
	{
		// if the element has a parent
		if ( ! empty($this->parent)) {
			$this->parent->replaceChild($this, $element);
		} else {
			throw new \BadMethodCallException(
				__METHOD__."() expects this element to have a parent element"
			);
		}
		
		return $this;
	}
155637
public function addElement(OrderElement $elements)
    {
        $this->elements[] = $elements;
        $elements->setOrder($this);

        return $this;
    }
1273836
public function order($column, $direction = 'asc')
    {
        $this->set('orderby', $column);
        $this->set('ordertype', $direction);

        return $this;
    }
737241
public function orderBy(string $field, int $direction = self::ASCENDING): DocumentSelector
    {
        return $this->sortBy($this->sortBy + [$field => $direction]);
    }
1064543
function reorder($expr, $direction = null)
    {
        $this->order(null);
        $this->order($expr, $direction);

        return $this;
    }
231959
public function orderBy(string $key, string $direction)
    {
        if (str_contains($key, '.')) {
            $fullPath          = explode('.', $key);
            $relationPath      = implode('.', array_slice($fullPath, 0, -1));
            $relationKey       = last($fullPath);
            $result            = array_get($this->relations, $relationPath, ['filters' => [], 'sorts' => []]);
            $result['sorts'][] = new OrderBy($relationKey, $direction);
            array_set($this->relations, $relationPath, $result);
        } else {
            $this->sorts[] = new OrderBy($key, $direction);
        }
        return $this;
    }
147283
protected function applySorting()
    {
        $sortable = app('scaffold.sortable');
        $element = $sortable->element();
        $direction = $sortable->direction();

        if ($element && $direction) {
            if (is_string($element)) {
                $this->assembler()->sort($element, $direction);
            }
        }

        return $this;
    }
1364707
public function sort($index, $direction)
    {
        if (!in_array($direction, ['ASC', 'DESC'])) {
            throw new CollectionException('Sort direction must be ASC or DESC.');
        }

        $this->_sort = ['index' => $index, 'dir' => $direction];
        return $this;
    }
512277
public function orderBy($column, $direction = IQueryBuilder::ORDER_ASC)
    {
        if ($direction == IQueryBuilder::ORDER_ASC || $direction == IQueryBuilder::ORDER_DESC) {
            $this->orderConditions[$column] = $direction;
        }

        return $this;
    }
8414
public function sortBy(string $field, string $direction = 'asc'): self
    {
        $this->sortBy = [$field, $direction];

        return $this;
    }
373693
private function move(Request $request, $direction)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
        $resource = $this->findOr404($configuration);

        $this->repository->$direction($resource);

        /** @var ResourceControllerEvent $event */
        $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);

        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
            throw new HttpException($event->getErrorCode(), $event->getMessage());
        }
        if ($event->isStopped()) {
            $this->flashHelper->addFlashFromEvent($configuration, $event);

            if ($event->hasResponse()) {
                return $event->getResponse();
            }

            return $this->redirectHandler->redirectToResource($configuration, $resource);
        }

        try {
            $this->resourceUpdateHandler->handle($resource, $configuration, $this->manager);
        } catch (UpdateHandlingException $exception) {
            if (!$configuration->isHtmlRequest()) {
                return $this->viewHandler->handle(
                    $configuration,
                    View::create(null, $exception->getApiResponseCode())
                );
            }

            $this->flashHelper->addErrorFlash($configuration, $exception->getFlash());

            return $this->redirectHandler->redirectToReferer($configuration);
        }

        $postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);

        if (!$configuration->isHtmlRequest()) {
            $view = $configuration->getParameters()->get('return_content', false) ? View::create(
                $resource,
                Response::HTTP_OK
            ) : View::create(null, Response::HTTP_NO_CONTENT);

            return $this->viewHandler->handle($configuration, $view);
        }

        $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);

        if ($postEvent->hasResponse()) {
            return $postEvent->getResponse();
        }

        return $this->redirectHandler->redirectToResource($configuration, $resource);
    }
75621
public function orderBy($field, $direction = 1)
    {
        if (is_string($direction)) {
            $direction = $direction === 'asc' ? 1 : -1;
        }

        $this->orderBy[$field] = $direction;

        return $this;
    }
452560
public function orderBy($column, $direction = 'ASC')
    {
        $orderVal         = [
            'ASC'  => 1,
            'DESC' => -1,
        ];
        $this->ordering[] = [
            'column'    => $column,
            'direction' => $orderVal[strtoupper($direction)],
        ];

        return $this;
    }
74648
public function withDirection(Direction $direction)
    {
        return new static($direction, $this->forced, $this->dryRun, $this->exceptionOnSkip, $this->custom);
    }