I have an agent that’s pulling a bunch of quotes and in the $Text of the agent, I’m generating a bulleted list of the authors of those quotes, which in this case is stored as a number in the custom attribute $PID.
You can’t sort a set; the ordering of sets is up to the computer. (Sets have to be able to answer the question “is X already in the set” very often, so it’s important for them to do this efficiently.)
But you can assign the value of a set to a list.
$MyList = $MySet
Lists can have duplicates. If you add an item to a list, there’s no need to check whether the item is already on the list. (Checking whether an item is in a list is O(n): you might have to look at every item already in the list. Checking whether an item is in a set is O(log n). So, it’s fine to sort lists.
Get us there without caching lots of list data in $MyList and $MySet. The extra parentheses around the collect call, i.e. (collect()).unique rather than collect().unique, is just to ensure Tinderbox properly evaluates the collect output before we start the chained actions.
As noted, I’m just ‘tiding’ here to avoid using caching attributes and further showing the upside of chainable dot-operators. As you code above works, there is no need to change it. However, in your original code, I’d suggest adding this at the end (bottom) of your template:
^action($MyList=;$MySet=;)^
That way, after the template has run, the two attributes you use are re-set to a default (empty) value. The attributes only store data during the export and avoid saving unnedded data for the long term.
The bigger part is getting the right output - as you did. The ‘tidying’ is just fun and more clarity for when you come to read the code many months later and avoid a “what the …?” moment trying to figure out what it means.
It took me a while to ‘get’ chained actions. When using them just check the order, things that need to be done first, come first. So here, the order of .sort and .unique don’t really matter, but .format() must come last.