How to sort this table primarily by count? (Code based on the Tag Manager)

176 views
Skip to first unread message

Teo Piitulainen

unread,
Aug 6, 2020, 4:42:50 AM8/6/20
to TiddlyWiki
Hello, I'm new here, and new to the code!

I use these Tag Manager-like Tiddlers to organize my tag hierarchies. For example, this one creates a table of all my wiki's "topics" tags and shows their count next to them (on the left).

Is there a way to sort the resultant table primarily by the count (most popular tag first), and secondarily by the tag title?

(Ideally clicking on the heading would sort the table by whichever sorting the user wants, but for now I would be very happy with just a default sort by count > title.)

Thanks for your attention!

Teo


\define lingo-base() $:/language/TagManager/


<table class="tc-tag-manager-table">
<tbody>
<tr>
<th><<lingo Count/Heading>></th>
<th><<lingo Tag/Heading>></th>
</tr>
<$list filter="[tags[]tag[topics]!is[system]sort[title]]">
<tr>
<td style="text-align: right"><$count filter="[all[current]tagging[]]"/
></td>
<td>{{||$:/core/ui/TagTemplate}}</td>
</tr>
<tr>
<td></
td>
<td colspan="1">
</td>
</
tr>

Eric Shulman

unread,
Aug 6, 2020, 5:33:05 AM8/6/20
to TiddlyWiki
On Thursday, August 6, 2020 at 1:42:50 AM UTC-7, Teo Piitulainen wrote:
Hello, I'm new here, and new to the code!

Welcome!
 
I use these Tag Manager-like Tiddlers to organize my tag hierarchies. For example, this one creates a table of all my wiki's "topics" tags and shows their count next to them (on the left).
Is there a way to sort the resultant table primarily by the count (most popular tag first), and secondarily by the tag title?

In the latest *pre*-release of the TWCore (v5.1.23) there is a new filter, "sortsub", which lets you sort items by a "subfilter" definition.
The subfilter uses filter syntax to compute a "derived value", based on the current tiddler being processed.

For your use-case, this does the job:
\define lingo-base() $:/language/TagManager/
\define sub() [tagging[]count[]]

<table class="tc-tag-manager-table">

<tr>
   
<th><<lingo Count/Heading>></th>
   
<th><<lingo Tag/Heading>></th>
<
/tr>
<$list filter="[tags[]tag[topics]!is[system]sort[title]!sortsub:integer<sub>]">

   <tr>
      <td style="text-align: right"><$count filter="[all[current]tagging[]]"/
></td>
     
<td>{{||$:/core/ui/TagTemplate}}</td>
   
</tr>
</
$list>
</table>

Notes:
* you will need to use the 5.1.23 pre-release, which you can get here: https://tiddlywiki.com/prerelease/empty
* because the subfilter syntax contains square brackets, it can't be "nested" inside the sortsub[...] syntax.  To work around this, use a macro (variable) to define the subfilter, and then reference it using the <...> variable syntax
* your posted table syntax had a few oddities that I cleaned up:
   1) there is no need for a <tbody> tag
   2) there was an extra row with empty <td>'s
   3) there was no terminating </$list> or </table>

You might also be interested in checking out my TagCloud "filter generator":

It sorts the tags by number of tagged items, but instead of displaying a table, it shows the count
included in the "tag pill" text, and also scales the size of the tag pill in proportion to the number
of items it is tagging.

enjoy,
-e

Teo Piitulainen

unread,
Aug 7, 2020, 11:37:37 AM8/7/20
to TiddlyWiki
Thank you, that solved it!
Thanks for fixing the oddities as well. :)

(The cloud view was actually quite unusable with some of my categories containing >100 entries and their counts ranging from 1 to triple digits :-) )

I was further wondering if the subfilter could be an even stricter filter, e.g. do the count only for "books", "papers", or an intersection like "English" + "books"?

This way, such tables could show the count distribution of any tag category (e.g. "topics") within any intersection of categories.
* Of course, clicking on the tag pill would still pop-up-list all the Tiddlers with that tag, even if the subfiltered count were more restricted.

Best regards,
Teo

Eric Shulman

unread,
Aug 7, 2020, 1:20:10 PM8/7/20
to tiddl...@googlegroups.com
On Friday, August 7, 2020 at 8:37:37 AM UTC-7, Teo Piitulainen wrote:
Thank you, that solved it!

YAY!
 

(The cloud view was actually quite unusable with some of my categories containing >100 entries and their counts ranging from 1 to triple digits :-) )


 In TiddlyTools/FilterGenerators/TagCloud, the tag-pill-styles() macro includes this line:
font-size:calc(0.5em + (0.2em * ($(count)$ / 5) ) );

The first number (0.5em) is the smallest font size to use.  The second number (0.2em) is the font size increment.  The third number (5) is the tag count for each group.

For the default values above:
The smallest tags will use 0.5em font size,
increasing to 0.7em if there are 5 or more tagged items,
increasing to 0.9em if there are 10 or more tagged items,
increasing to 1.1em if there are 15 or more tagged items,
etc.

If you have a wide range of tag counts, you can adjust the tag pill scaling calculation so the font size of the most frequent tags doesn't get too big.
In particular, if you make the tag count larger (e.g. by changing "5" to "25"), this will keep the scaling from increasing too much.
You could also make the font size increment smaller (e.g. by changing 0.2em to 0.1em).
font-size:calc(0.5em + (0.1em * ($(count)$ / 25) ) );

I was further wondering if the subfilter could be an even stricter filter, e.g. do the count only for "books", "papers", or an intersection like "English" + "books"?

This way, such tables could show the count distribution of any tag category (e.g. "topics") within any intersection of categories.
* Of course, clicking on the tag pill would still pop-up-list all the Tiddlers with that tag, even if the subfiltered count were more restricted.

Try these subfilters (one at a time, of course!):
\define sub() [tagging[]tag[books]count[]]
\define sub() [tagging[]tag[papers]count[]]
\define sub() [tagging[]tag[English]tag[books]count[]]

You might even be able to do "OR" combinations like this:
\define sub() [tagging[]tag[English]tag[books]] [tagging[]tag[French]tag[books]] +[count[]]

Let me know how it goes...

enjoy,
-e

teo...@gmail.com

unread,
Aug 22, 2020, 5:37:32 AM8/22/20
to TiddlyWiki
Thanks, that should make the TagCloud a realistic option! I'm still primarily interested in the table (e.g., for scrolling on a phone) though.

> Try these subfilters

Unfortunately that did not restrict the count (to within the added category). It also caused the table to be no longer sorted by the count, nor alphabetically. So I don't really understand what it did. :)

> You might even be able to do "OR" combinations like this

(OR combinations would also be cool!)

Now I also noticed that the first solution (which I said works) doesn't actually seem to sort alphabetically those Tiddlers with equal count. (I recall thinking that it did, but looking more closely, they seem random to me apart from the count as the primary sort.)

Thanks for your patience despite my delay - I don't always have a wiki day :)
Teo

Reply all
Reply to author
Forward
0 new messages