Sorting question

In my Kansas Railroad wiki, I have a tiddler to display all the values for the field statefiled along with how many railroads are associated with that statefiled value. The code is below:

<table class="table-borderless">
  <$list filter="[tag[Railroads]get[statefiled]unique[]]" variable="StateFiled">
    <tr>
      <td><<StateFiled>></td>
      <td style="text-align: right;"><$count filter="[tag[Railroads]statefiled<StateFiled>]"/></td>
    </tr>
  </$list>
  <tr>
    <td>Missing State</td>
    <td style="text-align: right;"><$count filter="[tag[Railroads]!has[statefiled]]"/></td>
  </tr>
</table>

The output looks like this:

What I would like to do and can’t quite put my finger on how to do it is to sort this output from high to low. For example, Kansas would be first, followed by Missouri, then Oklahoma, etc. I can’t help but think there is a slick way of doing this, but I’m not coming up with it. (Note: the Missing State row is separate and not included in this question.)

Thanks so much in advance!

Hi @HistoryBuff

Could the sortsub operator help you ?

Fred

This code seems to work:

\define myCount() [all[tiddlers]tag[Railroads]statefiled<currentTiddler>count[]]

<table class="table-borderless">
  <$list filter="[tag[Railroads]get[statefiled]unique[]] +[!sortsub:integer<myCount>]" variable="StateFiled">
    <tr>
      <td><<StateFiled>></td>
      <td style="text-align: right;"><$count filter="[tag[Railroads]statefiled<StateFiled>]"/></td>
    </tr>
  </$list>
  <tr>
    <td>Missing State</td>
    <td style="text-align: right;"><$count filter="[tag[Railroads]!has[statefiled]]"/></td>
  </tr>
</table>

Fred

I think [tag[Railroads]] should be stored into a variable with set-widget filter parameter since it is used 4 times in different filters. IMO it should be reused.

[tag[Railroads]] is the shortcut for [all[tiddlers]tag[Railroad]]

The count widget is very heavy. So if performance becomes a problem it should be optimized too, which will be tricky. I do not have a solution atm

Thanks @tw-FRed and @pmario,

The code worked great as written. Interstingly, if I took out all[tiddlers] from the myCount definition, it reverted back to the previous behavior. Not sure why.

Could elaborate on this, please?

Some operators are behaving like subfilters applied to their input, just like if the subfilter was starting by all[current]. I’ve read this somewhere in the forum, but can’t remember where… nor can I remember the list of operators behaving this way. At least subfilter and sortsub do IIRC.

So if you remove all[tiddlers] from myCount, everything works as if myCount was: [<StateFiled>tag[Railroads]statefiled<currentTiddler>count[]] which gives an empty result (or 0) for each value of StateFiled.

Fred

Oh sure. That makes perfect sense.