List sort by count field operator

137 views
Skip to first unread message

A Gloom

unread,
Aug 25, 2021, 7:05:57 AM8/25/21
to TiddlyWiki
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:

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

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

Soren Bjornstad

unread,
Aug 25, 2021, 9:16:08 AM8/25/21
to TiddlyWiki
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.

A Gloom

unread,
Aug 27, 2021, 10:47:27 PM8/27/21
to TiddlyWiki
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[]]

<small><ol><$list filter="[tag[zz@]each[zztopic]get[zztopic]subsort:integer<fldcntvar>]"><li><$view field="title"/> - <$count filter="[tag[zz@]zztopic<currentTiddler>]"/></li></$list></ol></small>

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

Soren Bjornstad

unread,
Aug 29, 2021, 9:10:08 AM8/29/21
to TiddlyWiki
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.

Jim

unread,
Aug 29, 2021, 9:45:15 AM8/29/21
to TiddlyWiki
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:


On Sunday, August 29, 2021 at 9:10:08 AM UTC-4 Soren Bjornstad wrote:
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.
teric wikiery would be greatly appreciated...
periandi.json

Mohammad Rahmani

unread,
Aug 30, 2021, 11:28:26 AM8/30/21
to tiddl...@googlegroups.com
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>>/>


Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/4feea918-07db-46d9-9489-a23cea65658dn%40googlegroups.com.

amreus

unread,
Aug 30, 2021, 3:28:21 PM8/30/21
to TiddlyWiki
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.

Mohammad Rahmani

unread,
Aug 30, 2021, 4:29:58 PM8/30/21
to tiddl...@googlegroups.com
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+



Best wishes
Mohammad


Mohammad Rahmani

unread,
Aug 30, 2021, 4:31:59 PM8/30/21
to tiddl...@googlegroups.com
On Mon, Aug 30, 2021 at 11:58 PM amreus <harl...@gmail.com> wrote:
Thank you Mohammad,

I have learned much from your wikis on github.

Glad they are useful ;-)

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 length process! Now you can use the wonderful sortsub!

 

amreus

unread,
Aug 30, 2021, 5:20:19 PM8/30/21
to TiddlyWiki
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!

A Gloom

unread,
Aug 30, 2021, 11:25:10 PM8/30/21
to TiddlyWiki
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 <<criteria>> 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 <currentTiddler> 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]
<li><$link/> (<$count filter="[tag[zz@AYoS]ayostopic<currentTiddler>]"/>) ::: <<criteria>></li>

[2]
<$vars criteria="[tag[zz@]zztopic<currentTiddler>count[]]">
<ul>
<$list filter="[tag[zz@]each[zztopic]get[zztopic]] +[!sortsub:integer<criteria>]">
<li><$link/> (<$count filter="[tag[zz@]zztopic<currentTiddler>]"/>) ::: <<criteria>></li>
</$list>
</ul>
</$vars>

Mohammad Rahmani

unread,
Aug 31, 2021, 1:13:02 AM8/31/21
to tiddl...@googlegroups.com
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



Best wishes
Mohammad


A Gloom

unread,
Aug 31, 2021, 3:04:13 PM8/31/21
to TiddlyWiki
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.
Reply all
Reply to author
Forward
0 new messages