Increment the sort order if one hasn't been already defined. This
ensures that new elements are created at the end of the list by default.

{@inheritDoc}
968334
public function onBeforeWrite()
    {
        parent::onBeforeWrite();

        if (!$this->Sort) {
            if ($this->hasExtension(Versioned::class)) {
                $records = Versioned::get_by_stage(BaseElement::class, Versioned::DRAFT);
            } else {
                $records = BaseElement::get();
            }

            $records = $records->filter('ParentID', $this->ParentID);

            $this->Sort = $records->max('Sort') + 1;
        }
    }
789527
public function applySort()
    {
        parent::applySort();

        if ($this->getSortBy() && $this->itemCreate) {
            //Remove previous click handler to Add new Item button and attach new one using sort argument.
            $this->container->js(true, $this->itemCreate->js()->off('click.atk_CRUD'));
            $this->container->js(true,
                                 $this->itemCreate->js()->on('click.atk_CRUD',
                                 new jsFunction([
                                     new jsModal('Add new', $this->pageCreate, [$this->name.'_sort' => $this->getSortBy()]),
                                 ]))
            );
        }
    }
442531
public static void reorder(int[] array, int[] order) {
        int[] original = copyOf(array);
        for (int i = 0; i < array.length; i++) {
            array[i] = original[order[i]];
        }
    }
924031
public function addSorting($sort, $order = SearchQuery::SORT_ASC)
    {
        $this->searchQuery->addSorting($sort, $order);

        return $this;
    }
1363987
public boolean inOrder() {
        SortedSet<Integer> prev = null;
        for (SortedSet<Integer> cell : cells) {
            if (prev == null) {
                prev = cell;
            } else {
                int first = cell.first();
                int last = cell.last();
                if (first > prev.first() && last > prev.last()) {
                    prev = cell;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
278042
public static <T> void appendOrder(OrderBy<T> orderBy, String orderByClause) {

        if (orderByClause == null) {
            return;
        }

        String[] chunks = orderByClause.split(",");
        for (String chunk : chunks) {
            String[] pairs = chunk.split(" ");
            Property p = parseOrderProperty(pairs);
            if (p != null) {
                orderBy.add(p);
            }
        }
    }
598075
public function increment($increment = null, $value = null)
    {
        if(!isset($this->options['increment']))  $this->options['increment'] = array();

        if(is_string($increment) && is_numeric($value))
            $this->options['increment'][$increment] = $value;
        elseif(is_array($increment) && is_null($value))
            foreach($increment as $k => $v)
                if(is_string($k) && is_numeric($v))
                    $this->options['increment'][$k] = $v;
        return $this;
    }
1097615
public function createNew($type = OrderTypes::TYPE_ORDER)
    {
        $order = parent::createNew();
        $order->setType($type);

        return $order;
    }
225252
public void setSortOrder(final java.lang.String sortOrder) {
        // when updating, make sure we store off the original so it may be
        // restored
        final String orig = (String) getStateHelper().get(PropertyKeys.origSortOrder);
        if (orig == null) {
            // do not call getSortOrder as it defaults to ascending, we want
            // null
            // if this is the first call and there is no previous value.
            getStateHelper().put(PropertyKeys.origSortOrder, getStateHelper().eval(PropertyKeys.sortOrder));
        }
        getStateHelper().put(PropertyKeys.sortOrder, sortOrder);
    }
107183
protected function setSortingOrder()
    {
        if (null !== $this->info->list->default_sort) {
            return $this;
        }

        if ($this->info->list->orderable && $this->info->list->getOrderableColumn()) {
            $this->info->list->default_sort = $this->info->list->getOrderableColumn();
        } elseif ($this->info->timestamps) {
            $this->info->list->default_sort = $this->info->timestamp_created;
        } elseif ($this->info->incrementing) {
            $this->info->list->default_sort = $this->model->getKeyName();
        }

        return $this;
    }
127306
public function incrementIndex($type, $increment = 1)
    {
        if ($type === 'new') {
            return $this->incrementIndexInNew($increment);
        }

        return $this->incrementIndexInOld($increment);
    }
127721
@Override
	public Integer next() {
		if (!hasNext()) {
			throw new NoSuchElementException(toString() + " ended");
		}

		current = next;
		next = next + increment;

		return current();
	}
1364403
protected function updateSortings(array $elements, int $lastSorting): int
    {
        foreach ($elements as $element) {
            if ($lastSorting > $element->sorting) {
                $element->sorting = ($lastSorting + 8);
                $element->save();
            }

            $lastSorting = (int) $element->sorting;
        }

        return $lastSorting;
    }
1014189
public function andSort(Sort $sort = null) : Sort
    {
        if ($sort === null) {
            return $this;
        }
        $orders = $this->orders;
        foreach ($sort as $order) {
            $orders[] = $order;
        }
        return new Sort($orders);
    }
1246210
private function incrementPositions(array $conditions, ?int $startPosition = null, ?int $endPosition = null): void
    {
        $columnName = $conditions['column'];

        $query = $this->connection->createQueryBuilder();

        $query
            ->update($conditions['table'])
            ->set($columnName, $columnName . ' + 1');

        if ($startPosition !== null) {
            $query->andWhere($query->expr()->gte($columnName, ':start_position'));
            $query->setParameter('start_position', $startPosition, Type::INTEGER);
        }

        if ($endPosition !== null) {
            $query->andWhere($query->expr()->lte($columnName, ':end_position'));
            $query->setParameter('end_position', $endPosition, Type::INTEGER);
        }

        $this->applyConditions($query, $conditions['conditions']);

        $query->execute();
    }
617315
private function incrementLikeCount()
	{
		$counter = $this->likeCounter()->first();
		
		if($counter) {
			$counter->count++;
			$counter->save();
		} else {
			$counter = new LikeCounter;
			$counter->count = 1;
			$this->likeCounter()->save($counter);
		}
	}
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);
        }
    }