Compare Operator, LessThan or Greater Than Macro

Hello everyone, I have a question regarding comparisons.

I would like to use a field for example quantity, volume, price, size etc. in some common tiddlers.

I would then like to compare the content of each tiddlers field(s) and either select the tiddler or flag it in some list.

A simple example would be to find the cheapest or dearest item in a list of ingredients or shopping items for instance and offer the cheapest or dearest item in a filtered list.

Can anyone suggest how this could be achieved please.

Looking at another post here re Mod of the Sticky-ToDo-Plugin: Filter to sort extracted strings by the date at the end of the string wanted - #10 by JanJo,
I see that maybe a sortsub operator may be of use.
https://tiddlywiki.com/#sortsub%20Operator

Scot

If I’m understanding you correctly, you’re asking for 2 things:

  1. Sort tiddlers by the numeric value of one of its fields (done via sortsub)
  2. Filter tiddlers by the numeric value of one of its fields (done via subfilter combined with compare)

For both of these situations you’ll notice that the operators contain a “suffix” that allows to specifically treat field values (which are inherently text) as numbers - that’s key to this working. Also similarly, they require a separate variable hold a filter to be referenced.

Example - cheapest 5 ingredients

<$vars price-sort="[get[price]]">
<$list filter="[tag[ingredient]sortsub:number<price-sort>limit[5]]">...</$list>
</$vars>

Example - filter ingredients less than $5

<$vars price-filter="[get[price]compare:number:lt[5]]">
<$list filter="[tag[ingredient]subfilter<price-filter>]">...</$list>
</$vars>

I’m a bit lazy so didn’t test this code, but if it doesn’t work, let me know

1 Like

Hi @stobot
thanks for your quick reply.
Your solutions both work well.
My intention was to have a Shopping List say with the cheapest price offered / selected and listing the specific retailer.

My items are selected (Checked) and shown against a list of retailers I use
What I want ultimately, is to put the price details in, and the cheapest option only to be shown against that particular retailer, at that point in time.

Bread and Rice Cheaper at Tesco, Beer, Lemonade, Noodles Cheaper at Asda

Tesco
Bread
Rice

Asda
Beer
Lemonade
Noodles

I am just experimenting at the moment and I’ll need to find a way to structure the tiddlers so that the ouput can be achieved; each item (cheapest) listed against the cheapest retailer at shopping time

Your solutions give me a place to start.
Thanks for your help
Scot

There are also ways to use the reduce operator and maths operators to discover the max or min price, then given that amount you can search for all appropriate tiddlers with that price in the required field. Remember there could be more than one item with the same min price.

Thanks @TW_Tones
Putting a bit more thought into this and coming to the conclusion that it would require a lot of work to keep things up to date.

I never knew about the reduce and math operators.

Its an interesting exercise though, thinking about how to structure the tiddlers to achieve what amounts to a price comparison site.
A simplified version for just a few items might suffice. The thought occurred to me as I was given some beer at Christmas, different stouts and IPA etc.
Keeping track of the best places to purchase each would save travelling between stores.

Scot

2 Likes