Filter with condition on numeric field

Hello,

The problem seems pretty simple but I’m lost again… Too long since I practised TW :sweat_smile:

I have tiddlers with a ‘priority’ field which is a numeric value: 0, 1, 2, 3. A tiddler with undefined priority field should be interpreted as priority 0.

Given a value N, I would like to create a filter which lists all the tiddler with priority higher or equal than N.

Thank you for your help!

1 Like

https://tiddlywiki.com/prerelease/#compare%20Operator

Have you tried compare operator

Thank you @arunnbabu81. Yes, I tried with compare, I tried something like:

<$vars N=2>
<$list filter="[all[tiddlers]get[priority]compare:number:gteq<N>]" >

</$list>
</$vars>

But I cannot find a way to deal with the case where ‘priority’ is not defined, and I don’t see how to obtain the names of the tiddlers instead of their priority?

Try this:

<$let n=2>
<$list filter="[all[tiddlers]] :filter[{!!priority}compare:integer:gteq<n>]"/>
</$let>

Notes:

  • n defines the desired priority value
  • The filter gets all tiddlers. Then, for each tiddler, it gets that tiddler’s priority field value and compares it as an integer to the desired priority value. If the priority field is missing or blank, it is automatically interpreted as a 0.
  • Note that the :filter[...] filter run takes tiddler titles as input, and gives tiddler titles as output. Within the filter run, the <currentTiddler> is automatically set to each input title.

enjoy,
-e

2 Likes

Thank you @EricShulman , this is great!

Do you think there is a nice way to take into account the case of tiddlers where priority is undefined, which should be considered as if it is 0? Or should I just deal with it as a special case outside the filter? (found the answer below)

Sorry, I found the answer to my previous question: I can simply use else

<$let n=2>
<$list filter="[all[tiddlers]] :filter[has[priority]{!!priority}else[0]compare:integer:gteq<n>]">

</$list>
</$let>

If you use get[priority] instead of {!!priority} then you don’t need to use has[priority] because the get[...] filter operator implicitly does the has[...] functionality (i.e., it only returns a result if the indicated field exists AND is not empty).

However… as I noted in my suggested solution, because the compare:integer[...] operator automatically treats invalid (non-numeric) values as 0, simply doing {!!priority}compare:integer:gteq<n> is sufficient for handling the case where priority is undefined OR empty (OR is actually 0), so neither the has[...] or else[0] parts are needed to achieve your desired result.

-e

2 Likes

Oh ok, I didn’t fully understand eariler… Thank you very much for clarifying Eric.