[tw5] List sort by count field operator

I saw references to sorting list results by the count operator but didn’t find the actual threads/posts about the method.

I assume it will require a subfilter, because in a root list filter the count just returns the count of the list results, not counts for each of the list results.

The sortsub operation worked with anything involving the titles of the list results (such as length) but not the count operator.

What I’m aiming for is ranking/sorting results (greatest to lowest counts) of such lists as tags and tiddlers tagged with each or

fields and how many tiddlers have each value of a particular field such as below:

    <$list filter="[tag[zz]each[zzfield1]get[zzfield1]]">
  1. <$view field="title"/> - <$count filter="[tag[zz] zzfield1]"/>

Any directions to threads about such esoteric wikiery would be greatly appreciated…

Unless I’m misunderstanding, sortsub[] with a filter containing count[] should do what you need. You might need to add the integer suffix to it though, or else it will sort the numbers alphabetically and not produce a very useful sort.

Thank you for your response, you have always been helpful.

I’m still working on it and studying your thread (Retrieving the number of link paths to a tiddler, https://groups.google.com/g/tiddlywiki/c/c_f9bfnp2KY/m/lMfu_WIpBAAJ ) where you ranked tiddlers by number of backlinks.

Where I’m going wrong-- I don’t think a single list/filter (without a subfilter or second filter) will let me sort by the count I’m aiming for, since the count of the root filter would just be the number of different values of the field being filtered, not the count of tiddlers having each different value. Putting in the each[]get[]count[] in the sortsub with any of the suffixes is producing any result.

What I’m starting out with, this gives an alphabetical listing (rather than the default listing without a sort which I assume is by field creation time but not sure if that’s so):

\define fldcntvar() [:filter[count[]]

    <$list filter="[tag[zz@]each[zztopic]get[zztopic]subsort:integer]">
  1. <$view field=“title”/> - <$count filter="[tag[zz@]zztopic]"/>
  2. </$list>

I also tried $set and $var for the sortsub filter but stuck with macro definition after seeing your work.

Can you maybe upload a small wiki with some tiddlers that could serve as an example? It’s pretty difficult to go into more depth without any data to try it on.

Not OP but in my case, I’d like to generate a list of surnames sorted by how many tiddlers share the same surname field.

Output should look similar to this:

  • Took (26)
  • Brandybuck (24)
  • Baggins (24)
  • Bolger (22)
  • Boffin (18)
  • Gardner (14)
  • Gamgee (7)
  • Burrows (7)

periandi.json (208 KB)

Jim,
Give a try with this script!

<ul>
<$list filter="[all[tiddlers]!is[system]each[surname]get[surname]]">
<li><$link/>(<$count filter="[all[tiddlers]surname<currentTiddler>]"/>)</li>
</$list>
</ul>

You can also replace <$link/> with <$text text=<<currentTiddler>>/>

Thank you Mohammad,

I have learned much from your wikis on github.

However, in your enthusiasm I think you over-looked the desire to sort the names by count.

I thought I saw an old post that showed a possible way by using an intermediate list, prefixing the count to the title with a delimiter, sorting the list, and finally splitting the count from the title - but I am unable to find it using search, and also do not recall if the method could be applied in this case.

1 Like

Ah, sorry I did not notice a sorted output was requested! This is the final solution with sort capability!

<ul>
<$vars criteria="[all[tiddlers]surname<currentTiddler>count[]]">
<$list filter="[all[tiddlers]!is[system]each[surname]get[surname]] +[!sortsub:number<criteria>]">
<li><$link/>(<$count filter="[all[tiddlers]surname<currentTiddler>]"/>)</li>
</$list>
</$vars>
</ul>

Note, this works in TW 5.1.23+

Thank you Mohammad,

I have learned much from your wikis on github.

Glad they are useful :wink:

However, in your enthusiasm I think you over-looked the desire to sort the names by count.

I thought I saw an old post that showed a possible way by using an intermediate list, prefixing the count to the title with a delimiter, sorting the list, and finally splitting the count from the title - but I am unable to find it using search, and also do not recall if the method could be applied in this case.

in TW 5.1.23+ life is much easier! no need to go through that lengthy process! Now you can use the wonderful sortsub!

Mohammad.

That is brilliant!

I made many attempts at a filter but never was able to get it. The missing piece for me is the currentTiddler in the criteria variable. In the context of the list, currentTiddler is the surname. It makes sense after I see it. This indicates I am still not understanding macros and variables correctly.

Thanks again!

Soren, sorry about the delay-- this something low on my list of too many things to do, so I haven’t been dedicating enough time to it. Let me export some shell tiddlers with the fields and values from the real world attempt at this issue.

Mohammad, good to see you responding as well. I tried your solution, it displayed the field values with counts but didn’t sort them by the counts (count of fields with each value). Playing with things-- I inserted a <> in the list so that each list result (see [1] beow) would show the criteria variable for each. It’s displaying: [tag[zz@]zztopiccount[]] **(note the " zztopiccount ", zztopic being the field name)

It appears that it’s not applying from the criteria $vars widget. The entire code (yours altered to work with my tiddlers selection) I used is below [2]. It displays what is expect but not sorting.

[1]

  • <$link/> (<$count filter="[tag[zz@AYoS]ayostopic]"/>) ::: <>
  • [2]
    <$vars criteria="[tag[zz@]zztopiccount[]]">

      <$list filter="[tag[zz@]each[zztopic]get[zztopic]] +[!sortsub:integer]">
    • <$link/> (<$count filter="[tag[zz@]zztopic]"/>) ::: <>

    A Gloom

    The code works with data provided by Jim and it also sorts!
    To give a try download data of Jim post above and drag and drop to tiddlywiki.com
    Next in a new tiddler paste the code of my last post you will see

    image.png

    Thank you Mohammad. I looked at Jim’s tiddlers and your script in TW official and went back and pasted your script into my wiki for another try with the bare minimum of alterations and it worked. I must had typoed during the first attempt. But now I have it working.

    Jim, very nice Hobbitology you created, others who create fantasy/ttRPG wikis should look at it. Also, this count script will work great for ranking by stats, achievements, etc-- ie leaderboards.

    Soren, thank you and sorry I didn’t get to putting up something for you to work with. But I’m collecting your back links ranking script, along with Mohammad’s field values count ranking and a tagged count ranking I based off them into one thread for Mohammad to add to TWScripts.