element will change the sorted order.
516392
public void setElement(int treap_node_index, int newElement) {
		if (m_comparator != null)
			m_comparator.onSetImpl_(this, treap_node_index);
		setElement_(treap_node_index, newElement);
	}
908374
static void applyOrdering(
            List<NodeInfo> order,
            Element compViewParent,
            Element positionSet,
            NodeInfoTracker tracker) {

        // first pull out all visible channel or visible folder children and
        // put their id's in a list of available children and record their
        // relative order in the CVP.

        final Map<String, NodeInfo> available = new LinkedHashMap<String, NodeInfo>();

        Element child = (Element) compViewParent.getFirstChild();
        Element next = null;
        int indexInCVP = 0;

        while (child != null) {
            next = (Element) child.getNextSibling();

            if (child.getAttribute("hidden").equals("false")
                    && (!child.getAttribute("chanID").equals("")
                            || child.getAttribute("type").equals("regular"))) {
                final NodeInfo nodeInfo = new NodeInfo(child, indexInCVP++);

                tracker.track(order, compViewParent, positionSet);

                final NodeInfo prevNode = available.put(nodeInfo.getId(), nodeInfo);
                if (prevNode != null) {
                    throw new IllegalStateException(
                            "Infinite loop detected in layout. Triggered by "
                                    + nodeInfo.getId()
                                    + " with already visited node ids: "
                                    + available.keySet());
                }
            }
            child = next;
        }

        // now fill the order list using id's from the position set if nodes
        // having those ids exist in the composite view. Otherwise discard
        // that position directive. As they are added to the list remove them
        // from the available nodes in the parent.

        Document CV = compViewParent.getOwnerDocument();
        Element directive = (Element) positionSet.getFirstChild();

        while (directive != null) {
            next = (Element) directive.getNextSibling();

            // id of child to move is in the name attrib on the position nodes
            String id = directive.getAttribute("name");
            child = CV.getElementById(id);

            if (child != null) {
                // look for the NodeInfo for this node in the available
                // nodes and if found use that one. Otherwise use a new that
                // does not include an index in the CVP parent. In either case
                // indicate the position directive responsible for placing this
                // NodeInfo object in the list.

                final String childId = child.getAttribute(Constants.ATT_ID);
                NodeInfo ni = available.remove(childId);
                if (ni == null) {
                    ni = new NodeInfo(child);
                    tracker.track(order, compViewParent, positionSet);
                }

                ni.setPositionDirective(directive);
                order.add(ni);
            }
            directive = next;
        }

        // now append any remaining ids from the available list maintaining
        // the order that they have there.

        order.addAll(available.values());
    }
193447
boolean handleSortOrder(final List<SortMeta> sortOrder) {
		boolean changed = false;

		if (sortOrder == null) {
			if (multiSortBy != null) {
				adaptee.clearSorting();
				multiSortBy = null;
				lastSorting = null;
				changed = true;
			}
		} else {
			final List<SortProperty> newSorting = convert2SortProperty(sortOrder);
			if (!newSorting.equals(lastSorting)) {
				adaptee.setSorting(newSorting);
				multiSortBy = sortOrder;
				lastSorting = newSorting;
				changed = true;
			}
		}

		return changed;
	}
247968
def sort
      element_order = params.fetch "element_order"

      element_order.each_with_index do |element_id, index|
        element = Element.find_by_id element_id
        element.update_attribute :position, index+1
      end

      head :no_content
    end
522424
private function updateSorting()
    {
        $ids = $this->getModelIds();
        // If no "next" sibling, simply increment the sorting as we are at the end of the list.
        if (!$this->marker) {
            foreach ($this->results as $model) {
                $this->position += 128;
                /** @var ModelInterface $model */
                $model->setProperty($this->getSortingProperty(), $this->position);
            }

            return;
        }

        $delta = $this->determineDelta();

        // Loop over all models and increment sorting value.
        foreach ($this->results as $model) {
            $this->position += $delta;
            /** @var ModelInterface $model */
            $model->setProperty($this->getSortingProperty(), $this->position);
        }

        // When the sorting exceeds the sorting of the "next" sibling, we need to push the remaining siblings to the
        // end of the list.
        if ($this->marker->getProperty($this->getSortingProperty()) <= $this->position) {
            do {
                // Skip models about to be pasted.
                if (\in_array($this->marker->getId(), $ids)) {
                    $this->marker = $this->siblingsCopy->shift();
                    continue;
                }

                $this->position += $delta;
                $this->marker->setProperty($this->getSortingProperty(), $this->position);
                $this->results->push($this->marker);

                $this->marker = $this->siblingsCopy->shift();
            } while ($this->marker);
        }
    }
987746
public function applySort()
    {
        if ($this->sortable === false) {
            return;
        }

        $sortBy = $this->getSortBy();

        if ($sortBy && $this->paginator) {
            $this->paginator->addReloadArgs([$this->name.'_sort' => $sortBy]);
        }

        $desc = false;
        if ($sortBy && $sortBy[0] == '-') {
            $desc = true;
            $sortBy = substr($sortBy, 1);
        }

        $this->table->sortable = true;

        if (
            $sortBy
            && isset($this->table->columns[$sortBy])
            && $this->model->hasElement($sortBy) instanceof \atk4\data\Field
        ) {
            $this->model->setOrder($sortBy, $desc);
            $this->table->sort_by = $sortBy;
            $this->table->sort_order = $desc ? 'descending' : 'ascending';
        }

        $this->table->on(
            'click',
            'thead>tr>th',
            new jsReload($this->container, [$this->name.'_sort' => (new jQuery())->data('column')])
        );
    }
231075
function setComparator() {
    // If the first three letters are "asc", sort in ascending order
    // and remove the prefix.
    if (by.substring(0,3) == 'asc') {
      var i = by.substring(3);
      comp = function(a, b) { return a[i] - b[i]; };
    } else {
      // Otherwise sort in descending order.
      comp = function(a, b) { return b[by] - a[by]; };
    }

    // Reset link styles and format the selected sort option.
    $('a.sel').attr('href', '#').removeClass('sel');
    $('a.by' + by).removeAttr('href').addClass('sel');
  }
1065593
function (collection, elements, settings, index) {
            for (var i = index + 1; i < elements.length; i++) {
                elements = swapElements(collection, elements, i - 1, i);
            }
            return collection.find(settings.elements_selector);
        }
256487
function(element, sortedList, sortFunction) {
			var lo = 0;
			var hi = sortedList.length - 1;
			var mid, result;

			while (lo <= hi) {
				mid = lo + Math.floor((hi - lo) / 2);

				result = sortFunction(element, sortedList[mid]);

				if (result < 0) {
					// mid is too high
					hi = mid - 1;
				} else if (result > 0) {
					// mid is too low
					lo = mid + 1;
				} else {
					// mid is the exact index of element
					return mid;
				}
			}

			return null;
		}
938320
function updateSortFunction() {
  switch(sortBy.value) {
    case '0': // receiverId increasing
      sortFunction = function(tr1, tr2) {
        if(tr1.getElementsByTagName('td')[0].textContent <
           tr2.getElementsByTagName('td')[0].textContent) {
          return -1;
        };
        return 1;
      }
      break;
    case '1': // receiverId decreasing
      sortFunction = function(tr1, tr2) {
        if(tr1.getElementsByTagName('td')[0].textContent >
           tr2.getElementsByTagName('td')[0].textContent) {
          return -1;
        };
        return 1;
      }
      break;
  }
  sortReceiversAndHighlight();
}
385530
def _append_sorted(root, el, comparator):
    
    for child in root:
        rel = comparator(el, child)
        if rel > 0:
            # el fits inside child, add to child and return
            _append_sorted(child, el, comparator)
            return
        if rel < 0:
            # child fits inside el, move child into el (may move more than one)
            _append_sorted(el, child, comparator)
    # we weren't added to a child, so add to root
    root.append(el)
454528
function() {
			this.writeDebug('sorting',arguments);
			var _this = this,
				$mapDiv = $('#' + _this.settings.mapID),
				$sortSelect = $('#' + _this.settings.sortID);

			if ($sortSelect.length === 0) {
				return;
			}

			$sortSelect.on('change.'+pluginName, function (e) {
				e.stopPropagation();

				// Reset pagination.
				if (_this.settings.pagination === true) {
					_this.paginationChange(0);
				}

				var sortMethod,
					sortVal;

				sortMethod = (typeof $(this).find(':selected').attr('data-method') !== 'undefined') ?  $(this).find(':selected').attr('data-method') : 'distance';
				sortVal = $(this).val();

				_this.settings.sortBy.method = sortMethod;
				_this.settings.sortBy.prop = sortVal;

				// Callback
				if (_this.settings.callbackSorting) {
					_this.settings.callbackSorting.call(this, _this.settings.sortBy);
				}

				if ($mapDiv.hasClass('bh-sl-map-open')) {
					_this.mapping(mappingObj);
				}
			});
		}
482764
function (order) {
			var items = {}, rootEl = this.el;

			this.toArray().forEach(function (id, i) {
				var el = rootEl.children[i];

				if (_closest(el, this.options.draggable, rootEl)) {
					items[id] = el;
				}
			}, this);

			order.forEach(function (id) {
				if (items[id]) {
					rootEl.removeChild(items[id]);
					rootEl.appendChild(items[id]);
				}
			});
		}
369327
private void addElementsToSortOrderMap(final Element element, int baseSortOrder) {
        elementSortOrderMap.addElement(element, baseSortOrder);
        final List<Element> castToChildElementList = castToChildElementList(element);
        // Increments the sort order index for each element
        int sortOrder = baseSortOrder;
        for (Element child : castToChildElementList) {
            sortOrder += SORT_ORDER_INCREMENT;
            addElementsToSortOrderMap(child, sortOrder);
        }
    }
51138
private function fixSorting(): void
    {
        $fixer = $this->file->getFixer();

        $fixer->beginChangeset();

        $firstTag = $this->removeOldTagLines();

        $fixer->addContent($firstTag['pointer'] - 1, $this->createNewSortedTagsContent());

        $fixer->endChangeset();
    }
308792
public void addElement(Element element, int sortOrder) {
        final String deepName = getDeepName(element);
        elementNameSortOrderMap.put(deepName, sortOrder);

    }
1086467
function (e) {
                var element = e.target;
                var sortableElement = element.isSortable === true ? element : findSortable(element);
                element = findDragElement(sortableElement, element);
                if (!dragging || !_listsConnected(sortableElement, dragging.parentElement) || addData(sortableElement, '_disabled') === 'true') {
                    return;
                }
                var options = addData(sortableElement, 'opts');
                if (parseInt(options.maxItems) && _filter(sortableElement.children, addData(sortableElement, 'items')).length >= parseInt(options.maxItems) && dragging.parentElement !== sortableElement) {
                    return;
                }
                e.preventDefault();
                e.stopPropagation();
                e.dataTransfer.dropEffect = store(sortableElement).getConfig('copy') === true ? 'copy' : 'move';
                debouncedDragOverEnter(sortableElement, element, e.pageY);
            }