Inspiration: https://stackoverflow.com/a/8598241/1348344

Sorts packages, then category or vice versa, depends on 2nd parameter

@param Change[] $changes
@return Change[]
438905
public function sort(array $changes, ?string $priority): array
    {
        // pur newer versions to the top, and "Unreleased" first
        usort($changes, function (Change $firstChange, Change $secondChange) use ($priority) {
            $comparisonStatus = $this->compareTags($firstChange, $secondChange);
            if ($comparisonStatus !== 0) {
                return $comparisonStatus;
            }

            if ($priority === self::PRIORITY_PACKAGES) {
                return $this->comparePackagesOverCategories($firstChange, $secondChange);
            }

            return $this->compareCategoriesOverPackages($firstChange, $secondChange);
        });

        return $changes;
    }
630656
public function setChanged($changed)
    {
        if (is_string($changed)) {
            $changed = new \DateTime($changed);
        }
        $this->getFileVersion()->setChanged($changed);

        return $this;
    }
608796
public function sortByDependencies() : self
    {
        /** @var $packages Package[] */
        $packages = $this->packages;
        $sortedPackages = [];

        while (!empty($packages)) {
            foreach ($packages as $packageName => $package) {
                $dependentPackages = $package->getDependencies()->toArray();
                if (empty(array_diff_key($dependentPackages, $sortedPackages))) {
                    // all of this packages dependencies are already in the sorted array, so it can be appended
                    $sortedPackages[$packageName] = $package;
                    unset($packages[$packageName]);
                    continue(2);
                }
            }
            throw new \LogicException('Packages have circular dependencies');
        }

        return new static($sortedPackages);
    }
948297
protected static function sortItems(array &$items)
    {
        usort($items, function ($a, $b)
        {
            if ($a['order'] === $b['order']) {
                return 0;
            }

            return ($a['order'] < $b['order']) ? -1 : 1;
        });
    }
664396
protected function getChanges(): Changes
    {
        if (null === $this->changes) {
            $this->changes = new Changes();
        }

        return $this->changes;
    }
1076551
public function setChanges($changes)
    {
        if ($changes instanceof Records) {
            $this->changes = $changes;
        } else {
            if (is_array($changes)) {
                $changes       = new Records($changes);
                $this->changes = $changes;
            }
        }
        return $this;
    }
1284566
public function sortPackageVersionEntities($packageVersions, $descending = false)
    {
        $keys = [];
        foreach ($packageVersions as $pv) {
            $keys[$pv->getVersion()] = $pv;
        }
        $sortedKeys = $this->sortPackageVersions(array_keys($keys), $descending);
        $result = [];
        foreach ($sortedKeys as $sortedKey) {
            $result[] = $keys[$sortedKey];
        }

        return $result;
    }
373938
protected function getListItems()
    {
        $changesets = ChangeSet::get();
        // Filter out published items if disabled
        if (!$this->config()->get('show_published')) {
            $changesets = $changesets->filter('State', ChangeSet::STATE_OPEN);
        }
        // Filter out automatically created changesets
        if (!$this->config()->get('show_inferred')) {
            $changesets = $changesets->filter('IsInferred', 0);
        }
        return $changesets
            ->filterByCallback(function ($item) {
                /** @var ChangeSet $item */
                return ($item->canView());
            });
    }
1315787
private function getMappedPackageList(array $packages): array
    {
        $groupedPackages = $this->groupPackagesByName($packages);

        $mappedPackages = [];
        foreach ($groupedPackages as $name => $packages) {
            $highest = $this->getHighestVersion($packages);

            $mappedPackages[$name] = [
                'highest' => $highest,
                'abandoned' => $highest instanceof CompletePackageInterface ? $highest->isAbandoned() : false,
                'replacement' => $highest instanceof CompletePackageInterface ? $highest->getReplacementPackage() : null,
                'versions' => $this->getDescSortedVersions($packages),
            ];
        }

        return $mappedPackages;
    }
1058218
protected static function sortApiVersions(array $versions = []): array
    {
        usort($versions, function ($first, $second) {
            $firstVersion = (float)$first['number'];
            $secondVersion = (float)$second['number'];

            if ($firstVersion == $secondVersion) {
                return 0;
            }

            return ($firstVersion > $secondVersion) ? 1 : -1;
        });

        return $versions;
    }
730658
private function reorderTabs($tabs): array
    {
        $orderedTabs = [];
        foreach ($tabs as $tab) {
            if ($tab instanceof OrderedTabInterface) {
                $orderedTabs[$tab->getIdentifier()] = $tab;
                unset($tabs[$tab->getIdentifier()]);
            }
        }

        uasort($orderedTabs, [$this, 'sortTabs']);

        return array_merge($orderedTabs, $tabs);
    }
52534
public List<ChangesContainer> getSortedList()
   {
      List<ChangesContainer> changesContainers = new ArrayList<ChangesContainer>(changes);
      Collections.sort(changesContainers);
      return changesContainers;
   }
1370091
protected function hasChanges($changes, $attributes = null)
    {
        // If no specific attributes were provided, we will just see if the dirty array
        // already contains any attributes. If it does we will just return that this
        // count is greater than zero. Else, we need to check specific attributes.
        if (empty($attributes)) {
            return count($changes) > 0;
        }

        // Here we will spin through every attribute and see if this is in the array of
        // dirty attributes. If it is, we will return true and if we make it through
        // all of the attributes for the entire array we will return false at end.
        foreach (Arr::wrap($attributes) as $attribute) {
            if (array_key_exists($attribute, $changes)) {
                return true;
            }
        }

        return false;
    }
909760
public static function sortArray(array $versions)
    {
        // Empty array does not needs sorting
        if (count($versions) === 0) {
            return array();
        }

        // Array of one item (pivot) from the middle
        $pivotArray = array_splice(
            $versions,
            (int) floor((count($versions) - 1) / 2),
            1
        );

        // Smaller/greater than pivot stack
        $smaller = array();
        $greater = array();

        // Fill stacks
        foreach($versions as $version) {
            if (Compare::greaterThan($version, $pivotArray[0])) {
                $greater[] = $version;
            } else {
                $smaller[] = $version;
            }
        }

        // Recurse and merge results
        return array_merge(
            self::sortArray($smaller),
            $pivotArray,
            self::sortArray($greater)
        );
    }
977089
protected function getSortsArray(array $sorts): array
    {
        $sortsArray = [];
        foreach ($sorts as $sort) {
            $sortsArray[] = ($sort->getOrder() === 'desc' ? '-' : '') . $sort->getField();
        }

        return $sortsArray;
    }
573657
protected function packChanges(bool $skipPrimary = false): array
    {
        if (!$this->hasChanges() && !$this->isSolid()) {
            return [];
        }

        if ($this->isSolid()) {
            //Solid record always updated as one big solid
            $updates = $this->packValue();
        } else {
            //Updating each field individually
            $updates = [];
            foreach ($this->getFields(false) as $field => $value) {
                //Handled by sub-accessor
                if ($value instanceof RecordAccessorInterface) {
                    if ($value->hasChanges()) {
                        $updates[$field] = $value->compileUpdates($field);
                        continue;
                    }

                    $value = $value->packValue();
                }

                //Field change registered
                if (array_key_exists($field, $this->changes)) {
                    $updates[$field] = $value;
                }
            }
        }

        if ($skipPrimary) {
            unset($updates[$this->primaryColumn()]);
        }

        return $updates;
    }
1213669
private function sortThemeLayoutUpdates(array $updates)
    {
        $directories = [];
        $files = [];
        foreach ($updates as $key => $update) {
            if (is_array($update)) {
                $update = $this->sortThemeLayoutUpdates($update);
                $directories[$key] = $update;
            } else {
                $files[] = $update;
            }
        }

        sort($files);
        ksort($directories);
        $updates = array_merge($files, $directories);

        return $updates;
    }