public function sortPluginsAndRespectDependencies(array $plugins, $pluginJsonCache = array())
    {
        $global = $this->getPluginsBundledWithPiwik();

        if (empty($global)) {
            return $plugins;
        }

        // we need to make sure a possibly disabled plugin will be still loaded before any 3rd party plugin
        $global = array_merge($global, $this->corePluginsDisabledByDefault);

        $global = array_values($global);
        $plugins = array_values($plugins);

        $defaultPluginsLoadedFirst = array_intersect($global, $plugins);

        $otherPluginsToLoadAfterDefaultPlugins = array_diff($plugins, $defaultPluginsLoadedFirst);

        // we still want to sort alphabetically by default
        natcasesort($otherPluginsToLoadAfterDefaultPlugins);

        $sorted = array();
        foreach ($otherPluginsToLoadAfterDefaultPlugins as $pluginName) {
            $sorted = $this->sortRequiredPlugin($pluginName, $pluginJsonCache, $otherPluginsToLoadAfterDefaultPlugins, $sorted);
        }

        $sorted = array_merge($defaultPluginsLoadedFirst, $sorted);

        return $sorted;
    }