Custom sort order

158 views
Skip to first unread message

Max Rozeman

unread,
Apr 30, 2021, 9:52:28 PM4/30/21
to TiddlyWiki
I use a 2-character prefix for tiddler headings, and 9 different 2-character prefixes for tiddler headings. Tiddler headings are often listed using <<list-links ... >>. The problem is that the tiddler headings need to be sorted in a particular non-alphabetical order: (SC, SP, ER, MV, IR, SN, CT, AC, CO). An easy solution would be to add prefixes to the prefixes, e.g.: (1 SC, 2 SP, 3 ER, ...) and sort headings using [sort[title]], but that would be messy. Is there a better way that doesn't require additional prefixes?

joost van den Dool

unread,
May 1, 2021, 5:53:38 AM5/1/21
to TiddlyWiki
Dear Max,

I had a similar problem with task statuses which I wanted to sort in a non-alphabetic way. I used the following:
<$list filter="[tag[Tasks]each[status]get[status]sortby{!!sortkey}]">

the sort key field was filled with the sortkeys separated by a space: next future done archived

I am not sure if this fully helps for the prefixes but hopefully it can set you in the right direction.

Kind regards,

Joost

Op zaterdag 1 mei 2021 om 03:52:28 UTC+2 schreef Max Rozeman:

PMario

unread,
May 1, 2021, 8:10:37 AM5/1/21
to TiddlyWiki
On Saturday, May 1, 2021 at 11:53:38 AM UTC+2 dode...@gmail.com wrote:
 
<$list filter="[tag[Tasks]each[status]get[status]sortby{!!sortkey}]">

Hi Max,

I'd use something like: [tag[asdf]sortby{testKeys}], where testKeys is a  single tiddler that contains the sort order

So tesKeys tiddler should contain a "space-separated" list eg: SC SP ER MV IR SN CT AC CO   ...  no commas!

With your filter, the sortkeys field needs to be part of every tiddler. .. That's a lot of redundant work, especially if you need to change something and you have 100+ tiddlers.

have fun!
mario

Max Rozeman

unread,
May 1, 2021, 9:35:51 AM5/1/21
to TiddlyWiki
Thank you for your replies, Joost and Mario.

For the solution suggested by Mario, I have created the testKeys tiddler with the space-separated list (without commas).

In another tiddler I tested it with the following: <<list-links filter:"[priority[1]sortby{!!testKeys}]">>  The output is in alphabetical order. I don't know what could be wrong or missing.

PMario

unread,
May 1, 2021, 10:08:16 AM5/1/21
to TiddlyWiki
<<list-links filter:"[priority[1]sortby{testKeys}]">> 

No exclamation marks. The have a meaning. See: more info in the docs.
-m

Max Rozeman

unread,
May 2, 2021, 4:36:56 AM5/2/21
to TiddlyWiki
Sorry -- that was a typing mistake, not a direct copy-paste. I did include the exclamation marks in my attempt, and the list was alphabetical. The documentation link is very useful more generally; -- there is more depth of information on that site there than I previously realised, so I'll read it more and hopefully learn to problem-solve better. I don't know where to start with this sorting problem, except reading about `sortby` and trying each content type for saving the `testKeys` tiddler. No success so far.

Max Rozeman

unread,
May 2, 2021, 8:56:47 AM5/2/21
to TiddlyWiki
I have found that the 2nd example of the sortby operator works, but only if the headings of the tiddlers to be listed and sorted are the same as and no longer than the prefixes. They are prefixes however, i.e. at the beginning of tiddler titles; not complete headings. Complete tiddler headings may for example look like: "SP. Tiddler heading" or "ER. Another tiddler heading".

Max Rozeman

unread,
May 3, 2021, 5:48:41 AM5/3/21
to TiddlyWiki
Well it looks like this isn't as simple as I had hoped. Maybe what I need is a macro, but that's a skill I've yet to learn. For the time being then I'll just have to sort my titles the easy and messy way by adding prefixes to my prefixes.

PMario

unread,
May 3, 2021, 9:05:12 AM5/3/21
to TiddlyWiki
On Monday, May 3, 2021 at 11:48:41 AM UTC+2 Max Rozeman wrote:
Well it looks like this isn't as simple as I had hoped. Maybe what I need is a macro, but that's a skill I've yet to learn. For the time being then I'll just have to sort my titles the easy and messy way by adding prefixes to my prefixes.

You are right. My code doesn't work as expected. ... I did very poor testing, so it looked like it would have worked. ... :/ Sorry for that.

But the sortby operator needs "matching titles" it doesn't use regexp matches internally, which would have been needed for your usecase.

So imo there won't be a "out of the box" solution. ... At least I don't know it.

-mario

Soren Bjornstad

unread,
May 3, 2021, 2:30:56 PM5/3/21
to TiddlyWiki
Here's a solution using sortsub, which sorts based on a key obtained by running a filter against each input item.  This assumes your prefixes are separated from the rest of the tiddler title by a space. If not, you may have to change the split character or get a bit more creative:

\define myfilt() [all[current]split[ ]first[]match[SP]then[010] [all[current]split[ ]first[]match[SC]then[020]] [all[current]split[ ]first[]match[ER]then[030]]

<<list-links "[tag[yourtag]sortsub<myfilt>]">>

The subfilter in myfilt uses a series of runs to convert each prefix into a numerical value which sorts in the order you want. You can add as many steps to the filter as you need to handle all your prefixes. Any items that don't have a prefix listed in the filter will get no sort key and so show up at the top of the list.

I am sure you could get creative and figure out how to store the mapping in a data tiddler and dynamically build the filter, but I'll let someone else figure that out if they're so inclined.

Soren Bjornstad

unread,
May 3, 2021, 2:34:21 PM5/3/21
to TiddlyWiki
Sorry, there's a typo in that macro, should be:

\define myfilt() [all[current]split[ ]first[]match[SP]then[010]] [all[current]split[ ]first[]match[SC]then[020]] [all[current]split[ ]first[]match[ER]then[030]]

Max Rozeman

unread,
May 4, 2021, 2:42:44 AM5/4/21
to TiddlyWiki
Thank you very much Soren -- it works beautifully! Thanks also for your clear explanation.

Max Rozeman

unread,
May 4, 2021, 2:46:31 AM5/4/21
to TiddlyWiki
No problem Mario. I've learned from each of the replies to my question so I'm glad you posted.
Reply all
Reply to author
Forward
0 new messages