src/Sortable.php
public function getSortVal($targetId, $position = 'after', $groupingId = null)
    {
        if (!$position || !in_array($position, ['after', 'before'])) {
            throw new \Exception('You must specify a valid position: "after" or "before".');
        }

        if (false === ($sortVal = $this->deriveSortVal($targetId, $position, $groupingId))) {
            $this->rebuildSortAfter($targetId, $position != 'after', $groupingId);
            $sortVal = $this->deriveSortVal($targetId, $position, $groupingId);

            if (!$sortVal) throw new SortableException(
                'Sort value can not be derived. Check if all sort values in the same scope are unique.'
            );
        }

        return $sortVal;
    }
public function getSortVal($targetId, $position = 'after', $groupingId = null)
    {
        if (!$position || !in_array($position, ['after', 'before'])) {
            throw new \Exception('You must specify a valid position: "after" or "before".');
        }

        if (false === ($sortVal = $this->deriveSortVal($targetId, $position, $groupingId))) {
            $this->rebuildSortAfter($targetId, $position != 'after', $groupingId);
            $sortVal = $this->deriveSortVal($targetId, $position, $groupingId);

            if (!$sortVal) throw new SortableException(
                'Sort value can not be derived. Check if all sort values in the same scope are unique.'
            );
        }

        return $sortVal;
    }
Derives next sort value for the record to be inserted after or before $targetId.
If it necessary, the sort value of records following the $targetId will be reset automatically.

@param int $targetId Record id after or before which a new record supposed to be inserted.
@param string $position The possible options are: 'after', 'before'. Specifies how to interpret the $targetId.
@param null|int $groupingId Id of the grouping entity. If it not passed the $targetId will be used in a sub-query
to derived groupingId value. It has sense only if $this->grpColumn is not null.
@return int
@throws SortableException