public function sort()
    {
        if (!$this->request->is('ajax') || !$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        if (empty($this->request->data)) {
            throw new BadRequestException();
        }

        // save the new language positions
        $languages = $this->Languages->patchEntities(
            $this->Languages->find('allFrontendBackend'),
            $this->request->data
        );
        /** @var Connection $connection */
        $connection = $this->Languages->connection();
        $connection->begin();
        foreach ($languages as $language) {
            if (!$this->Languages->save($language)) {
                $connection->rollback();
                break;
            }
        }
        if ($connection->inTransaction()) {
            $connection->commit();
            $status = 'success';
            $flashMessage = __d('wasabi_core', 'The language position has been updated.');
        } else {
            $status = 'error';
            $flashMessage = $this->dbErrorMessage;
        }

        // create the json response
        $frontendLanguages = $this->Languages
            ->filterFrontend(new Collection($languages))
            ->sortBy('position', SORT_ASC, SORT_NUMERIC)
            ->toList();
        $backendLanguages = $this->Languages
            ->filterBackend(new Collection($languages))
            ->sortBy('position', SORT_ASC, SORT_NUMERIC)
            ->toList();

        $this->set(compact('status', 'flashMessage', 'frontendLanguages', 'backendLanguages'));
        $this->set('_serialize', ['status', 'flashMessage', 'frontendLanguages', 'backendLanguages']);

        $this->RequestHandler->renderAs($this, 'json');
    }