public function sort()
    {
        if ($this->sortedPackages === null) {
            $this->sortedPackages = [];
            reset($this->unsortedPackages);
            while (!empty($this->unsortedPackages)) {
                $resolved = $this->sortPackage(key($this->unsortedPackages));
                if ($resolved) {
                    reset($this->unsortedPackages);
                } else {
                    next($this->unsortedPackages);
                }
            }
        }

        return $this->sortedPackages;
    }