Sort nodes of the category in the order in which they should be displayed.

@see \core_user\output\myprofile\tree::sort_categories()
@throws \coding_exception
340678
public function sort_nodes() {
        $tempnodes = array();
        $this->validate_after_order();

        // First content noes.
        foreach ($this->nodes as $node) {
            $after = $node->after;
            $content = $node->content;
            if (($after == null && !empty($content)) || $node->name === 'editprofile') {
                // Can go anywhere in the cat. Also show content nodes first.
                $tempnodes = array_merge($tempnodes, array($node->name => $node), $this->find_nodes_after($node));
            }
        }

        // Now nodes with no content.
        foreach ($this->nodes as $node) {
            $after = $node->after;
            $content = $node->content;
            if ($after == null && empty($content)) {
                // Can go anywhere in the cat. Also show content nodes first.
                $tempnodes = array_merge($tempnodes, array($node->name => $node), $this->find_nodes_after($node));
            }
        }

        if (count($tempnodes) !== count($this->nodes)) {
            // Orphan nodes found.
            throw new \coding_exception('Some of the nodes specified contains invalid \'after\' property');
        }
        $this->nodes = $tempnodes;
    }
1123933
public function getRootCategoryWithChildren(CategoryInterface $category)
    {
        if (null === $category->getContext()) {
            throw new \RuntimeException('Context cannot be null');
        }
        if (null !== $category->getParent()) {
            throw new \RuntimeException('Method can be called only for root categories');
        }
        $context = $this->getContext($category->getContext());

        $this->loadCategories($context);

        foreach ($this->categories[$context->getId()] as $contextRootCategory) {
            if ($category->getId() === $contextRootCategory->getId()) {
                return $contextRootCategory;
            }
        }

        throw new \RuntimeException('Category does not exist');
    }
993146
function ( ) {
				_PinCategories = _PinCategories.sort ( 
					function ( a, b ) {
						return a.CategoryName.localeCompare 
						( 
							b.CategoryName,
							_Translator.UserLanguage
						);
					} 
				);
			}
765068
public function sort(ModelInterface $category, array $data = []): PodiumResponse
    {
        $forumSorter = $this->getSorter();

        $forumSorter->setCategory($category);

        if (!$forumSorter->loadData($data)) {
            return PodiumResponse::error();
        }

        return $forumSorter->sort();
    }
67441
public function actionSort() 
    {
        $this->layout = static::FORM_LAYOUT;
        $modelName = $this->getCompatibilityId();
        if(isset($_POST[$modelName]) && count($_POST[$modelName])) {
            //If we are changing the main categories then use a different algorithm
            if($_GET['Menu_id']==-1) {
                foreach($_POST[$modelName] as $key => $id) {
                    $record = $this->findModel($id);
                    $record->sort_order = $key+1;
                    $record->save();
                }
            } else {
                $parent = $this->findModel($_GET['Menu_id']);
                foreach($_POST[$modelName] as $key => $id) {
                    $record = $this->findModel($id);
                    $record->appendTo($parent);
                }
            }
            return $this->redirect(['index']);
        } else {
            $this->getSearchCriteria();
            $searchCriteria = ['MenuSearch' => ['Menu_id' => $this->searchModel->Menu_id]];
            $this->searchModel = new $this->MainModelSearch;
            $this->dataProvider = $this->searchModel->search($searchCriteria);

            $this->dataProvider->pagination = false;
            return $this->render('sort', [
                'searchModel' => $this->searchModel,
                'dataProvider' => $this->dataProvider,
            ]);         
        }
    }
722460
public function buildCategoryTree()
    {
        $categories = $this
            ->categoryRepository
            ->getAllCategoriesSortedByParentAndPositionAsc();

        $categoryTree = [
            0 => null,
            'children' => [],
        ];

        /**
         * @var CategoryInterface $category
         */
        foreach ($categories as $category) {
            $parentCategoryId = 0;
            $categoryId = $category->getId();

            if (!$category->isRoot()) {
                if ($category->getParent() instanceof CategoryInterface) {
                    $parentCategoryId = $category->getParent()->getId();
                } else {

                    /**
                     * If category is not root and has no parent,
                     * don't insert it into the tree.
                     */
                    continue;
                }
            }

            if ($parentCategoryId && !isset($categoryTree[$parentCategoryId])) {
                $categoryTree[$parentCategoryId] = [
                    'entity' => null,
                    'children' => [],
                ];
            }

            if (!isset($categoryTree[$categoryId])) {
                $categoryTree[$categoryId] = [
                    'entity' => null,
                    'children' => [],
                ];
            }

            $categoryTree[$categoryId]['entity'] = $category;

            $categoryTree[$parentCategoryId]['children'][] = &$categoryTree[$categoryId];
        }

        return $categoryTree[0]['children']
            ?: [];
    }
653009
public function sortCategories(array $items)
    {
        $repository = $this->getRepository();
        $em         = $this->getDoctrineHelper()->getEntityManager();

        foreach ($items as $item) {
            $parent = $repository->find($item['parent']);
            $child  = $repository->find($item['id']);
            if (null !== $child) {
                $child->setParent($parent);
                $child->setHierarchy($item['weight']);
                $em->persist($child);
            }
        }

        $em->flush();
    }
1132527
protected function ensure_category_loaded() {
        if (is_array($this->_categories)) {
            return; // Already done.
        }
        if (is_null($this->_course)) {
            throw new coding_exception('Attempt to get the course category for this page before the course was set.');
        }
        if ($this->_course->category == 0) {
            $this->_categories = array();
        } else {
            $this->load_category($this->_course->category);
        }
    }
936235
function createCategories() {
    $cnt = count( $this->categories );
    if( 0 >= $cnt )
      return;
    $output = null;
    foreach( $this->categories as $category ) {
      $attributes = $this->_createParams( $category['params'], array( 'LANGUAGE' ));
      if( is_array( $category['value'] )) {
        foreach( $category['value'] as $cix => $categoryPart )
          $category['value'][$cix] = $this->_strrep( $categoryPart );
        $content  = implode( ',', $category['value'] );
      }
      else
        $content  = $this->_strrep( $category['value'] );
      $output    .= $this->_createElement( 'CATEGORIES', $attributes, $content );
    }
    return $output;
  }
990983
public function category(Category $category)
    {
        $this->category = $category;

        $this->category->categorise($this);
    }
1056226
protected function magic_get_category() {
        $this->ensure_category_loaded();
        if (!empty($this->_categories)) {
            return reset($this->_categories);
        } else {
            return null;
        }
    }
385838
public function sort(): void {
        $r = new Stack();
        while (!$r->isEmpty()) {
            $tmp = $this->pop();

            while ((!$r->isEmpty()) && (Comparator::lessThan($r->peek(), $tmp))) {
                $this->push($r->pop());
            }
            $r->push($tmp);
        }
        while (!$r->isEmpty()) {
            $this->push($r->pop());
        }
    }
542379
final public function sort(array $attributes = array())
    {
        $category       = $this->find($attributes['movingId']);
        $positionEntity = $this->find($attributes['movedId']);

        if ($category instanceof Category) {
            if ($this->fireEvent('sorting', array($category)) === false) {
                return false;
            }

            $category->setCaller($this);
            $category->{$attributes['type']}($positionEntity);

            $this->fireEvent('sorted', array($category));

            return $category;
        }

        throw new Exception(sprintf(Exception::CANT_UPDATE_MODEL,
            'Subbly\\Model\\Category',
            $this->name()
        ));
    }
850839
def sorted_by(by = nil)
      if block_given?
        categories.sort { |a, b| yield(b[1]) <=> yield(a[1]) }
      else
        categories.sort { |a, b| send(by, b[0]) <=> send(by, a[0]) }
      end
    end
1208106
protected function iterateActiveCategories($categories)
    {
        foreach ($categories as $category) {
            $this->activeCategory[] = $category->id;
            if (isset($category->get_childActive)) {
                $this->listActiveCategories($category->get_childActive);
            }
        }
    }
994662
public function setCategories($categories)
    {
        foreach ($categories as $key => $name) {
            $this->_categories[$key] = $name;
        }
    }
956091
public static function getRootCategories()
    {
        $app = App::getInstance();

        $sql = $app['safesql']->query(
            "SELECT
				`id`
			FROM `category`
			WHERE hidden = 0
			AND id > 0
			AND parent IS NULL
			AND deleted = 0
			ORDER BY `order` ASC",
            array());
        $results = $app['db']->get_results($sql);
        $cats = array();

        if (!is_null($results)) {
            foreach ($results as $cat) {
                $cats[] = new Category($cat->id);
            }
        }
        return $cats;
    }