Group Sites by Groups
You can use the Site component

PHP

$groups = Craft::$app-&gt;getSites()-&gt;getAllGroups();
foreach ($groups as $group){
    echo $group-&gt;name;
    foreach ($group-&gt;getSites() as $site){
        echo $site-&gt;name;
    }
}


Twig

{% set groups = craft.app.getSites().getAllGroups() %}
{% for group in groups %}
    {{ group.name }}
    {% for site in group.getSites() %}
        {{ site.name }}
    {% endfor %}
{% endfor %}