Hi, I would like to display all tiddlers with a certain tag and sort them primarily by the field course. Many tiddlers share the same field value for course and I want to sort these by the field section.
As you can see, the course MATH100 does not sort right (for what I need). I’m not sure how TW sorts things when it sees two or more tiddlers with same field value. (I’m guessing by creation field?)
Is there a way to specify the secondary sort field? For example, it would be nice if we can do something like this (for the nonexisting operator multisort).
This is a little bit hard to do without sample tiddlers to work with, and I’m already past my bedtime , so no time for me to enter a decent number of tiddlers for testing.
That said, the following seems to work for me with three sample tiddlers I quickly entered. See if a modified version (i.e. modified to work for your tiddlers) works for you:
(this does require courses to have a standard/consistent number of characters; the “enlist” is just my quick and dirty way of getting my tiddlers)
The 3 spaces I add between the two should be sufficient to be sure that first field will always be preponderant over second field. I choose space as it it the lowest ascii code beyond control chars. One could add return carriage too (a single one is enough in the context of single line field).
I think this called doubly sorted table! You can convert this to a macro and for example sort in ascending order by first column and descending order for second column!
So the trick to do doubly sort by field x and y (and perhaps more) is to concatenate these field values (value of x)(value of y) and then sort this concatenation as string, or number. This is clever. Thank you!
Both approaches have their own benefits. The plus with the filter operators like sortsub[], filter[] etc that accept a filter is that you can use multiple filter runs for sorting or filtering for even more complex logic!
I had kept my original code slightly simpler by assuming course and section codes would be consistent sizes.
For a generic version of this code to work with anything, consider adding “pad” to whatever number of fields involved in sorting. A “pad[20],[a]” or something like that would do the trick, replacing that “20” with something that is maybe 10-20 characters bigger than the max number of characters you might expect for that field.