Finds the previous element based on position.

@param Collection        $elements
@param SortableInterface $current
@param bool              $next Whether to look for the next or the previous

@return mixed
760884
private function findSibling(Collection $elements, SortableInterface $current, $next = false)
    {
        if ($next) {
            $sibling = $elements->filter(function (SortableInterface $s) use ($current) {
                return $s->getPosition() > $current->getPosition();
            })->first();
        } else {
            $sibling = $elements->filter(function (SortableInterface $s) use ($current) {
                return $s->getPosition() < $current->getPosition();
            })->last();
        }

        return $sibling ?: null;
    }
612767
public function removeAll($elements)
    {
        if ($elements instanceof CollectionInterface) {
            $elements = $elements->toArray();
        }
        $this->elements = array_values(
            array_udiff(
                $this->elements,
                $elements,
                function ($a, $b) {
                    if ($a === $b) {
                        return 0;
                    } elseif (is_int($a) && is_object($b)) {
                        return -1;
                    } elseif (is_object($a) && is_int($b)) {
                        return 1;
                    } elseif ($a < $b) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            )
        );
        foreach ($this->subLists as $subList) {
            $subList->removeAll($elements, true);
        }
        return $this;
    }
894363
public function next()
    {
        // fetch next entry.
        // if we have no entrys anymore, we add false (which is
        // returned by shiftEntry()) so current() will complain.
        if (count($this->_iteratorCache) - 1 <= $this->count()) {
            $this->_iteratorCache[] = $this->shiftEntry();
        }

        // move on array pointer to current element.
        // even if we have added all entries, this will
        // ensure proper operation in case we rewind()
        next($this->_iteratorCache);
    }
839569
public function orderElements($arr) {
        $result = true;

        // Create a new array
        $newarr = array();
        if (!empty($arr)) {
            $currentelement = null;
            // Get the element without previous
            foreach($arr as $key => $element) {
                if (!$element->getPrevious()) {
                    $currentelement = $arr[$key];
                    $newarr[0] = $arr[$key];
                }
            }
            if (!$currentelement) {
                $result = false;
            }
            // Follow the next rules
            $counter = 1;
            while ($result && $currentelement->getNext()) {
                $i = $this->findObjectInArray($currentelement->getNext(), $arr);
                $currentelement = $arr[$i];
                $newarr[$counter] = $arr[$i];
                $counter++;
            }
            // Compare number of elements between original and new array
            if ($result && count($arr) != count($newarr)) {
                $result = false;
            } else if ($newarr) {
                $result = $newarr;
            } else {
                $result = false;
            }
        } else {
            $result = array();
        }
        return $result;
    }
626443
public function next()
    {
        try {
            $this->current = $this->map->successor($this->current);
        } catch (\OutOfBoundsException $e) {
            $this->current = null;
        }

        $this->index++;
    }
780244
public function getOrderNumber($id, $currentQuery = null)
    {
        $item = $this->findOne($id);
        if (!isset($item)) return 0;

        if (empty($currentQuery)) {
            $query = self::find()->orderBy($this->orderBy);
        } else {
            $query = clone $currentQuery; //!
        }
        foreach ($this->orderBy as $prio_field => $direction) {
            $item_prio = $item[$prio_field];
            $where = [$direction == SORT_ASC ? '<=' : '>=', $prio_field, $item_prio];
            $query = $query->andWhere($where);
        }
        $num = $query->count();
        return intval($num);
    }
508155
protected function NextItem()
	{
		while (true)
		{
			if ( ! is_null( $this->childIter ) )
			{
				if ( $this->childIter instanceof ExprIterator )
				{
					$result = $this->childIter;
					$this->childIter = null;
					return $result;
				}
				else
				{
					if ( $this->childIter->MoveNext() )
						return $this->childIter->getCurrent();
					else
						$this->childIter = null;
				}
			}
			if ( ! $this->iter->MoveNext() )
				return null;

			if ( $this->owner->MoveNext( $this->provider, $this->dataPool, $this->iter->getCurrent(), $res ) )
			{
				if ( $res instanceof XPath2NodeIterator )
				{
					$this->childIter = $res;
				}
				else
				{
					$this->childIter = null;
					return $res instanceof XPathItem
						? $res
						: XPath2Item::fromValue( $res );
				}
			}
		}
	}
1198568
public function getChildOf($current_page, $elements)
    {
        $child_of = 0;

        if (is_a($current_page, '\WP_Post')) {
            $current_page = $current_page->ID;
        }

        foreach ($elements as $key => $element) {
            if (isset($element->ID) && isset($current_page) && $element->ID == $current_page) {
                $child_of = $element->ID;
                unset($elements[$key]);

                if (intval($element->menu_item_parent) > 0) {
                    $child_of = $this->getChildOf(intval($element->menu_item_parent), $elements);
                }

                break;
            }
        }

        return $child_of;
    }
1027811
protected function NextItem()
	{
		while ( $this->iter->MoveNext() )
		{
		    if ( $this->iter->getSequentialPosition() == $this->position )
		    {
		    	$this->iter->ResetSequentialPosition();
		        return $this->iter->getCurrent();
		    }
		}
		return null;
	}
716454
public function moveBlockUp( $currentIndex )
    {
        $array =& $this->attributes['blocks'];

        $newIndex = $currentIndex - 1;

        if ( $newIndex < 0 || $newIndex >= count( $array ) )
            return false;

        $tmpItem = $array[$newIndex];

        $array[$newIndex] =& $array[$currentIndex];
        $array[$currentIndex] =& $tmpItem;

        if ( $tmpItem->toBeRemoved() )
            $this->moveBlockUp( $newIndex );

        return true;
    }
23547
protected function NextItem()
	{
		if ( $this->used ) $this->iterator->next();
		$this->used = true;
		return $this->iterator->current(); // getCurrent();
	}
739243
public function getOrdered($collection, $sort = null)
    {
        if (!$sort) {
            return $collection;
        }

        $unordered = [];
        foreach ($collection as $content) {
            $unordered[$content->getId()] = $content;
        }

        $ordered = [];
        foreach ($sort as $id) {
            if (isset($unordered[$id])) {
                $ordered[] = $unordered[$id];
            }
        }

        return $ordered;
    }
903609
public void next() {
        boolean hasLeft = currentLeft != null;
        boolean hasRight = currentRight != null;

        if (!hasLeft && !hasRight) {
            throw new NoSuchElementException();
        }

        int order;
        if (hasLeft && !hasRight) {
            order = -1;
        } else if (!hasLeft) {
            order = 1;
        } else {
            order = comparator.compare(currentLeft, currentRight);
        }

        if (order < 0) {
            reportedLeft = currentLeft;
            currentLeft = nextOrNull(left);
            reportedRight = null;
        } else if (order > 0) {
            reportedLeft = null;
            reportedRight = currentRight;
            currentRight = nextOrNull(right);
        } else {
            reportedLeft = currentLeft;
            reportedRight = currentRight;
            currentLeft = nextOrNull(left);
            currentRight = nextOrNull(right);
        }
    }
426368
private function getNextItem(Tag $item, $items)
    {
        $currentItem = $items[0];

        while ($currentItem !== null and $currentItem !== $item) {
            $currentItem = next($items);
        }

        $next = next($items);

        return $next !== false ? $next : null;
    }
799667
protected function getExpectedOrder(array $elements): array
    {
        global $sortOrder;

        $sortOrder = array_reverse($this->sortMap);
        usort($elements, [$this, "compare"]);

        return $elements;
    }
459596
private function searchNext($currentArticleId, $listUrl)
    {
        if (null === $listUrl) {
            return null;
        }

        $page = $this->listApi->get($listUrl, $this->requestToListUrlConverter->getListSpotName());
        $items = $page->getItemList();
        if (0 === count($items)) {
            return null;
        }

        $keys = array_keys($items);
        $positionCurrentElement = array_search($currentArticleId, $keys);
        if (false === $positionCurrentElement) {
            return null;
        }

        $positionNextElement = $positionCurrentElement + 1;
        // if we are the last element, we need to get the first of the next page
        if ($positionNextElement === count($keys)) {
            $nextPageUrl = $page->getNextPageUrl();
            if (null === $nextPageUrl) {
                return null;
            }
            $nextPage = $this->listApi->get($nextPageUrl, $this->requestToListUrlConverter->getListSpotName());
            $nextItems = $nextPage->getItemList();
            if (0 === count($nextItems)) {
                return null;
            }
            reset($nextItems);

            return array('item' => current($nextItems), 'pageUrl' => $nextPageUrl);
        }

        return array('item' => $items[$keys[$positionNextElement]], 'pageUrl' => $listUrl);
    }
135531
private function orderByPriority(array $elements)
    {
        // Drop keys from the elements array
        $elements = array_values($elements);
        $length = count($elements);
        for ($i = 0; $i < $length; $i++) {
            for ($j = $i; $j < $length; $j++) {
                $priorityFirst = isset($elements[$i]['priority']) ? (int) $elements[$i]['priority'] : 1;
                $prioritySecond = isset($elements[$j]['priority']) ? (int) $elements[$j]['priority'] : 1;

                if ($priorityFirst < $prioritySecond) {
                    $aux = $elements[$j];
                    $elements[$j] = $elements[$i];
                    $elements[$i] = $aux;
                }
            }
        }

        return $elements;
    }