Sorting using variable

Hi ,

I am trying to sort a result of a filter using a numeric value that is the output of another filter stored in a variable

some reshuffling does happen, its just not the correct order
the code i am using is below , is there something particularly wrong that you can spot.

> \define totalfieldx()
> <$set name= total-field-x filter="[kin::from<currentTiddler>!tag[Completed]has:field[fieldY]] :reduce[get[fieldx]add<accumulator>]">
<<totalfieldx>>
> \end
> 
> <$list filter="  [fieldY[somedata]]+[sortsub:integer<totalfieldx>]"><li>
>  <$link><$view field="title" />  <<totalfieldx>>

I don’t see a matching </$set> for your <$set>.

Also, I don’t think you can insert a macro like that into a filter, but maybe there’s been some update I missed. When I try it with my own test, I get back the actual text defining the macro.

1 Like

Try this:

<$let totalfieldx="[kin::from<currentTiddler>!tag[Completed]has:field[fieldY]] :reduce[get[fieldx]add<accumulator>]">
<$list filter="[fieldY[somedata]] +[sortsub:integer<totalfieldx>]">
   <li>
   <$link to=<<currentTiddler>>>
      <$view field="title" /> <$text text={{{ [subfilter<totalfieldx>] }}}/>
   </$link>
   </li>
</$list>
</$let>

Notes:

  • totalfieldx is a variable containing filter syntax, NOT filter results
  • The sortsub filter operator takes filter syntax as it’s operand value. It then evaluates that syntax for each input item and sorts those items by the resulting values.
  • Similarly, [subfilter<totalfieldx>] is used in order to evaluate the totalfieldx syntax and display the result for each item.
  • The $link widget needs the to=<<currentTiddler>> param to correctly link to the current title. If this param is omitted, it will try to link to a tiddler title based on the inner content of the $link widget, which in this case will include the totalfieldx value for the current title, resulting in a link to a missing tiddler.
  • Always be sure to properly match each widget or HTML element with a corresponding </$widgetname> or </HTMLelement>

Note also that I am not able to test this potential solution, since A) I don’t have the kin filter plugin installed, and B) I don’t have any example data to operate on. In the future, it would be much easier for others to test/debug any suggested solutions if you provide a link to an online TiddlyWiki as an MTC (“minimal test case”)… perhaps hosted at https://tiddlyhost.com

Let me know if this helps…

-e

1 Like

Hi Eric /mark

Thank you for the infomration

i have put an example on tiddly host https://mighty-soap-71.tiddlyhost.com/
tiddler C is tagged with B , and tiddler B is tagged with tiddler X

the resulting running totals from the kin filter as you can see below are not sorted properly after the corrections

image

I think the use of “reduce” filter run in your original run was causing the sortsub operator to get confused. I simplified it to use the “sum” operator, and then used the “sort” filter run to do the sorting.

A subtlety with the subfilter operator is that it is not a constructor. So it needs something in front of it to give valid results. In this case, it appears to need to have the current tiddler set (thats what the “all[current]” does). This is somewhat unexpected, because in some cases the subfilter operator works without the preceding “all[current]”.

<$let totalfieldx="[kin::from<
currentTiddler>!tag[Completed]has:field[fieldY]get[fieldx]sum[]]">
<$list filter="[fieldY[Y]]:sort[subfilter<totalfieldx>]">
   <li>
   <$link to=<<currentTiddler>>>
      <$view field="title" /> <$text text={{{ [all[current]subfilter<totalfieldx>] }}}/>
   </$link>
   </li>
</$list>
</$let>
1 Like

Thanks mark,

it does work , however after further testing i noticed that the sorting has a strange way of lining the results up once the totals gets past 10

totals of 1 ,and 10 to 20 come first , then 2, and 20 to 30 , and so forth , 1 to 10 comes last!

its not random there is some logic to it, its just not clear what is controlling it , could this be caused by the kin filter?

i have added tiddly map for better visuals

thanks again

Tiddlywiki doesn’t sort numbers in numerical order by default: it looks at all the first digits, then all the second digits (if present) and so on. Replacing :sort with :sort:number should fix this behavior (and for simpler filter runs, the nsort operator does the same thing.)

The sort filter run operator is pretty new, and it’s one of the few that lets you modify its behavior through extended syntax. Here’s the documentation, in case you have any more complicated sorting needs!

3 Likes

Thank you all ,

:number fixed it , i also added :reverse to have them sorted largest to smallest

thanks again for your help

1 Like