Hello,
In a blog template, I have a tagcloud that I’d like to sort by the number of instances of each tag.
I’ve tried a lot of possibilities and scoured the docs in every direction but don’t seem to find the right approach.
My code in the controller:
// fetch all tags
$tags = $articles->pluck('tags', ',', true);
// order tags by number of occurences in blog
$tags = $tags->sortBy($articles->filterBy('tags', $tag, ',')->count(), $direction = 'desc');
sortBy doesn’t work here. I’ve tried using php usort() and all other sort functions I could think of to no avail.
The way I think I could solve this would be to add the number of instances of each tag in the tags array but I can’t find how to do it and it’s killing me!!!
I’m sure someone will have faced this before me and can share their insights (which will make me look like the php rookie I am).
Also, I think, you’re using the filerBy() method not correctly. In the code you posted, you’re filtering by comparing the field tags with the value ,, by using the operator $tag!? I don’t understand this. https://getkirby.com/docs/cheatsheet/pages/filter-by
The syntax is actually correct to filter with splitting (see the tags solution in the docs). However you are right that this is missing from the cheat sheet. I will add it, thanks for pointing that out.
That is explained in the PHP docs you linked to above. Basically PHP will pass in two tags and your function needs to compare them and return either -1, 0 or 1 depending on which value is larger. That’s just how sorting works in computer science.
Thanks a lot for helping out on this!
I had to swith $a and $b to get the order in descending but it all worked out!
I turned my head around your solution and the countless examples before using it and I think I got a good unuderstanding of it. Interesting stuff!
Thanks again for your help
You can also return -strcmp($aCount, $bCount) (note the minus), that will reverse the order as well and might be easier to understand for other people reading your code.