If you create a new Collection from your tags, you can then map a virtual sort field, where the number you assign to this field is then number of articles with this tag. You can then sort by this field.
$tags = $albums->pluck('tags', ',', true);
$tags = new Collection($tags);
$sortedTags = $tags->map(function($item) {
$item->sortNo = 'here the number of children';
return $item;
})->sortBy('sortNo');
Ok, so theoretically the code I posted above should do exactly the right think, only it seems that this doesn’t work with Kirby 3 collections anymore. I’ll come up with a solution.
$albums = $page->children()->listed();
$tags = $albums->pluck('tags', ',', true);
// flip the array to prevent duplicate keys
$tags = array_flip($tags);
// assign the number of albums with that tag to the value
array_walk($tags, function(&$value, $key) use($albums) {
return $value = $albums->filterBy('tags', $key, ',')->count();
});
// reverse sort the array
arsort($tags);
dump($tags);
You can now loop through the array, and if you want, assign the number of occurrences