Sort on two date fields combined

The following filter produces a list of articles that have either a ‘published-on’ date or a ‘republished1’ date:

[!republish1[]] [!published-on[]] +[tag[Article]]

I want to sort it on both the date fields combined. So that the latest date in either ‘published-on’ or ‘republished1’ comes to the top.

What sort operator do I use?

There is a sortsub operator that has the following example:

[tag[Field Operators]sortsub:date<compare-by-newest-of-modified-and-created-dates>]

But I am not able to translate this into my requirement.

Hi @deshmukh

If the republished1 field (if it exists) is always later than the published-on field, could you sort like this?

<$vars sub=[get[republished1]else{!!published-on}]>

{{{[all[tiddlers]tag[Article]sortsub:date<sub>]}}}

</$vars>

Otherwise something like

<$vars sub=[get[republished1]else[0]compare:integer:gt{!!published-on}else{!!published-on}]>

Best,
Chris

@deshmukh I think @clutterstack response is inspired, by understanding your data ie; republished is always after published, as modified is always after created, it is easier to c ode solutions.

However your question asks how to sort on a calculated value, in this case two values. If that were necessary (it is not in this case) the prerelease introduces the Sort Filter run prefix.

There are other was to achieve the same results pre-5.2.0 but they are less strait forward.

Yet in your example, surely if the field republished even exists it will be more recent than the published?

@TW_Tones Good point that we have a new filter run prefix to make this sort of thing less mind-bending.

I’m curious to see if someone comes along with the obvious simple alternative to my subfilter that gets the greater of the published-on and republished1 field values.

(I tried and failed)

And a small error on my part: my final list was in ascending date order, not latest-first, so it should be amended to use !sortsub. A test list incorporating this looks like:

<$list filter="[all[tiddlers]tag[Article]!sortsub:date<sub>]">

{{!!title}} - {{!!published-on}} - {{!!republished1}} - {{{[all[current]subfilter<sub>]}}}
</$list>

Yeah, no I thought I made the subfilter shorter but not really. As @TW_Tones said, there’s more than one way to get there, but I didn’t find a better one.

1 Like

@TW_Tones I got what you are saying and read up the documentation.

But solution by @clutterstack did solve the problem. Republished is always after published. So, there was no problem.

1 Like

@clutterstack Thanks. This worked perfectly.

1 Like