protected function sort($assets)
    {
        $original = $assets;
        $sorted = [];

        while (count($assets) > 0) {
            foreach ($assets as $handle => $asset) {
                // No dependencies anymore, add it to sorted
                if (!$asset->hasDependency()) {
                    $sorted[$handle] = $asset;
                    unset($assets[$handle]);
                } else {
                    foreach ($asset->getDependency() as $dep) {
                        // Remove dependency if doesn't exist, if its dependent on itself,
                        // or if the dependent is dependent on it
                        if (!isset($original[$dep]) or $dep === $handle or (isset($assets[$dep]) and $assets[$dep]->hasDependency($handle))) {
                            $assets[$handle]->removeDependency($dep);
                            continue;
                        }

                        // This dependency hasn't been sorted yet
                        if (!isset($sorted[$dep])) {
                            continue;
                        }

                        // This dependency is taken care of, remove from list
                        $assets[$handle]->removeDependency($dep);
                    }
                }
            }
        }

        return $sorted;
    }