Retrieving the name of the most recent child note

A follow up to this post

I’ve found that retrieving the date of the most recent child helps in identifying containers with recent notes. I’m wondering if there is a way to also include the $Name of the most recent note, not only the date? I can use collect().max because it’s dedicated to find the most recent date. I want the Note $Name associated with that date.

Of the top of my head (assumption: all $Name values in a container are unique):

$MyString = collect(children, $Name).sort($Modified).at(-1);

We collect the children’s $Name values, sort the list on the $Name’s $Modified value making the newest one last, then take the last list item and store it in $MyString, i.e. the $Name of the most recently modified child. You don’t have to use $MyString, by all means employ a user String-type attribute.

1 Like

Thanks @mwra. I did not realise that sort() can be applied to a list of note names after the collect() has been applied. All nicely documented here in aTBRef.

I’m wondering if a more robust approach would be to use the paths of each note (this is a case where group designator listing the unique IDs for each note would be useful). This does the same thing but returns the path

 collect(children,$Path).sort($StartDate).at(-1)

Unfortunately these two expressions do not work out of the box

collect(children,$Path).sort($StartDate).at(-1).$Name;
$Name(collect(children,$Path).sort($StartDate).at(-1));

although the 2nd expression works fine if I do it in two steps e.g. store the path to most recent child note and then do $Name($Path)

Using paths? Yes! $Path is generally more robust than $Name. But, if you have two notes with the same name in the same container, only $ID tells them apart (so a good reason to avoid non-unique $Name, especially if using action code a lot).

Not in one go, but you can do

$SomeList = collect(children,$ID)
// use $SomeList where you want the list of IDs

This fails due to incorrect syntax - you can’t simply use ad hoc chaining. So, $Name won’t be understood, and anyway without it the whole proceeding code already represents the value.

The second case logically should work but Tinderbox sometimes needs to ‘crystallise’ the output of one expression as a value beforf using it in another expression. I deleted my test do for the above but you can try either extra parentheses around the collect and chained items:

$Name((collect(children,$Path).sort($StartDate).at(-1)));

This should (may!) tell Tinderbox to extract the path value before fetching the $Name at that $Path. Otherwise, if you don’t want kitty litter in an interim attribute use (v9+ var single-line declaration syntax):

var vPath = collect(children,$Path).sort($StartDate).at(-1);
// then use $Name(vPath)
// var is lost as soon as code, i.e. rule, stamp, etc., has run

More on ID as group designator.