[tw5] How to sort a list by multiple fields (and put it in a table)?

Hi,

I have a question about sort a list by multiple fields. I have a bunch of entries of persons’ bios. Each bio entry has a set of fields associated, such as birth year, death year, birth date, tenure length etc.

I want to create a list with some of those persons and sort it by multiple fields, such as first by tenure length, if same tenure, then sort by birth year, then death year etc.

What I currently have is

<$list filter="[c_status[incumbent]sort[c_rank]]">
</$list>

This gives me a list that allows me to filter all incumbents but the list can only be sorted by rank. I want to add multiple fields (e.g. V1, V2, V3) behind the rank sort but it doesn’t work. I’d appreciate it if any one could share their insights on this.

My second, maybe more unrealistic goal, is to transform the list to a table. Its visual would ideally look like this (https://en.wikipedia.org/wiki/Lists_of_state_leaders_by_age#Top_ten_since_1942), i.e. I can put each field value of one entry in different cells of the same row and the table will update itself

The code I’m currently using is

<$list filter="[c_status[incumbent]sort[c_rank]]">
<$link to=<>><$view field=birth_year/><$view field=title/><$view field=birth_month/><$view field=c_rank/></$link>

And the layout is a list of sorted entries with field values crudely attached at the beginning and the end of each line. Basically I wonder if I can separate those values into different cells.

Many thanks!

To sort by multiple fields, you would use the sort filter run prefix.(SFRP) Inside the SFRP, you would use a filter perhaps like:

:sort:string:caseinsensitive[[c_rank]addsuffix[tenure]addsuffix[birth_year]]

which would sort by rank, tenure, and birth year. You might need to zero-pad your fields.

To create the table, the easiest way is to use standard HTML notation.

Hi Mark,

Thanks for the help! I tried to copy your code, but it gives a syntax error notice. All my fields used for sorting are numerical values, do I still need to treat them as string in the code? (e.g. 1960.02 for birth_year, 50 for rank)

What I have now is

<$list filter="[c_status[incumbent]:sort:number[[rank]addsuffix[birth_year]]]">
<$link to=<<currentTiddler>>><$view field=birth_year/><$view field=title/><$view field=rank/></$link><br>
</$list>

or


<$list filter="[c_status[incumbent]:sort:string:caseinsensitive[[rank]addsuffix[birth_year]]">
<$link to=<<currentTiddler>>><$view field=birth_year/><$view field=title/><$view field=rank/></$link><br>
</$list>

Neither code seems to work and I’m having some difficulty in reading through the SFRP document. Would anyone mind helping me to look more into this? Many thanks!

‘Mark S.’ via TiddlyWiki <tiddlywiki@googlegroups.com> 于2022年4月16日周六 10:00写道:

I typed too quickly. It is hard to help without access to the data in use. However, this worked in my tests:

<$list filter="[c_status[incumbent]]:sort:string:caseinsensitive[{!!rank}addsuffix{!!birth_year}]">
<$link to=<<currentTiddler>>> <$view field=birth_year/> <$view field=title/> <$view field=rank/></$link><br>
</$list>

Note the use of {!!rank} to get the rank value.

You will likely get even more answers if you use https://talk.tiddlywiki.org/ , which is now the primary forum for TiddlyWiki.

HTH