[TW5] Secondary sort in Sortable Tables

122 views
Skip to first unread message

michaelha...@gmail.com

unread,
Mar 31, 2019, 5:18:27 PM3/31/19
to TiddlyWiki
Hello, I have been using Jed McCarty's code for clickable table column headers to create sortable tables.

I recently put together a table to keep track of my various ebooks and from whih vendor I purchased them. See code below.

\define SortableFilters()
[tag[EBook]has[vendor]$(SortType)$sort{!!sort_by}]
\end

\define ToggleDirection()
<$list filter='[<currentTiddler>sort_type[!]]' emptyMessage="""<$action-setfield sort_type='!'/>""">
   
<$action-setfield sort_type=''/>
</$list>
\end

<$set name=SortType filter='[<currentTiddler>get[sort_type]]'>
<table>
<tr>
  <th>
    <$button class='tc-btn-invisible' style='width:100%;height:100%'>
      Title
      <$action-setfield sort_by='title'/
>
   
<<ToggleDirection>>
   
</$button>
  </
th>
 
<th>
   
<$button class='tc-btn-invisible' style='width:100%;height:100%'>
     
Author
     
<$action-setfield sort_by='author'/>
   
<<ToggleDirection>>
   
</$button>
  </
th>
 
<th>
   
<$button class='tc-btn-invisible' style='width:100%;height:100%'>
     
Vendor
     
<$action-setfield sort_by='vendor'/>
   
<<ToggleDirection>>
   
</$button>
  </
th>
</tr>
<$list filter=<<SortableFilters>>>
<tr>
  <td>
    /
/<$link to={{!!title}}><$view field="title"/></$link>//
 
</td>
  <td>
    <$view field='
author'/>
 
</td>
  <td>
    <$view field=
'vendor'/>
  </td>
</tr>
</$list>
</table>
</$set>

I have noticed that, after sorting the table by a column, the secondary sort is always by title. So if I sort the list by vendor, the books are listed within that sort alphabetically. As this is a library index, it would be more appropriate if I could have the secondary sort default to the author field, rather than the tiddler titles.


Is there any way I might be able to change the behavior of the table, so that it will sort by a specific field rather than the title, after I have used the proper sort_by?


Any suggestions would be very much appreciated.

Thank you.

S. S.

unread,
Mar 31, 2019, 7:29:10 PM3/31/19
to TiddlyWiki
Huw asked a similar question just yesterday, and who else but Jed had the answer.
Filter list by tag but sort by and show value of field
Hope that helps.
Cheers


michaelha...@gmail.com

unread,
Mar 31, 2019, 7:56:24 PM3/31/19
to TiddlyWiki
S. S.,

I apologize for being a clod, but I am afraid I don't see how I would be able to implement the get operator in the above table.

Would you be able to explain how I would use the operator properly? From what I understand, I would not have thought it would work with the sortable table.

Thank you.

S. S.

unread,
Mar 31, 2019, 8:12:44 PM3/31/19
to TiddlyWiki
I had imagined that changing the first line of the macro to the below might have done it.
But I may be very mistaken.
[tag[EBook]has[vendor]get[author]$(SortType)$sort{!!sort_by}]
I have not used the get operator before, so this may be very incorrect help.
Sorry.

michaelha...@gmail.com

unread,
Mar 31, 2019, 8:36:46 PM3/31/19
to TiddlyWiki
No, unfortunately that has removed all content except the authors. Guess I will have to keep playing around with it.

Thank you for suggesting the get operator. Can you think of anything else that might work?

Thank you.

S. S.

unread,
Mar 31, 2019, 9:47:31 PM3/31/19
to tiddl...@googlegroups.com
Give this a try:

<table>
<$list filter="[tag[Ebook]has[vendor]each[vendor]get[vendor]sort[]]">
<$list filter="[tag[Ebook]has[vendor]field:vendor
<currentTiddler>sort[author]]">
<tr>
 
<td>
    //<$link to={{!!title}}><$view field="title"/></$link>//
 
</td>
 
<td>
    <$view field='author'/>
 
</td>
 
<td>
    <$view field='vendor'/>
 
</td>
</tr>

</$list>
</$list>
</table>


Edited to fix one tag from book  to  Ebook  .

Mohammad

unread,
Apr 1, 2019, 12:57:46 AM4/1/19
to TiddlyWiki
Added to TW-Scripts

Mohammad

unread,
Apr 1, 2019, 12:59:36 AM4/1/19
to TiddlyWiki
S.S,
 What does each operator do here?


On Monday, April 1, 2019 at 6:17:31 AM UTC+4:30, S. S. wrote:

S. S.

unread,
Apr 1, 2019, 2:01:39 AM4/1/19
to TiddlyWiki

Mohammad,

Well, Michaelha hasn't confirmed that it works yet!

What I did was to test a similar scenario in my own TiddlyWiki.

I have a bunch of books tagged book, that have an author field, and a (publication) year field.
Some authors have multiple books.

So I tried to list my books so that it sorted first by the Author name (author field), then subsorted the books of an author by the date of publication (date field).

The below did it quite nicely!

<table>
<$list filter="[tag[book]each[author]get[author]sort[]]">
<$list filter="[tag[book]field:author
<currentTiddler>sort[date]]">
<tr>
 
<td>
    //<$link to={{!!title}}><$view field="title"/></$link>//
 
</td>
 
<td>
    <$view field='author'/>
 
</td>
 
<td>

    <$view field='date'/>
 
</td>
</tr>
</$list>
</$list>
</table>

\define Explanation()

The 1st $list filter:
  •  Gets a list of all my books (tagged book)
  •  Looks at the author field and gets a list of authors using the get operator
    • Since the get operator is one of the few operators that allow duplicates, it is possible to have the same author listed multiple times in the list created
  • So I first use the each operator to ensure unique items (authors).
  • Then this list of all authors is sorted by the author's name
    •  As we know, sort[] sorts by <<currentTiddler>> which at this point is the author's name in the author field
The 2nd $list filter will now run for each author in the list created by the 1st $list
  • For the first item (<<currentTiddler>> - which is the first author's name)
    • It first gets a list of all the books (tagged book)
    • Then looks at the author field - selects the ones that match this first author (<<currentTiddler>>)
    • Then sorts those ( sort[date] ) by the publication date (date field)
  • Then it does that for the 2nd author in the list provided by the 1st $list filter
  • . . . and so on for all the authors one by one as provided by the 1st $list filter.
\end Explanation

I now have a nicely tabled list!
I'm glad Michaelha asked the question, as I like the way all my books are listed now.

Cheers

Mohammad

unread,
Apr 1, 2019, 2:27:33 AM4/1/19
to TiddlyWiki
Hi S.S.,
 Many thanks for detailed explanation! I got why you have used each operator!
This is actually a doubly sorted list! 
Wonderful!

Added to TW-Scripts

--Mohammad

S. S.

unread,
Apr 1, 2019, 2:37:05 AM4/1/19
to TiddlyWiki
Ha ha!!!!

I thought you asked - what does each operator do here.
Now I believe you actually meant - what does the "each" operator do here!

Still, it was fun to explain what each operator was doing.

Have a great semantic day!

Mohammad

unread,
Apr 1, 2019, 2:41:05 AM4/1/19
to TiddlyWiki
:-)

HaHaHa......

USEFUL confusion!  :-)

TonyM

unread,
Apr 1, 2019, 3:32:48 AM4/1/19
to TiddlyWiki
Folks,

The most important concept to achieve sorts and subsorts is the use of nested list widgets. The filter in each list widget can select and sort. If the outer widget does the initial sort, the nested list can do the subsort.

this nesting can go multiple levels deep but quickly exhausts the data you have in each level.

Regards
Tony

michaelha...@gmail.com

unread,
Apr 1, 2019, 9:25:31 AM4/1/19
to TiddlyWiki
S. S.,

Firstly, thank you for taking the time to put that table together. I have tried it and it does work ... if the table is not sortable. 

Perhaps I have explained myself poorly. What I am looking to do is to keep the table sortable, but change the default behavior, which is that, after the sort_by is set, the tiddlers are arranged by the title field. For my purposes, I would like to change this behavior and for the tiddlers to be arranged by the author field. 

Perhaps this is not possible. I do not know how it might be done, which is why I thought to ask. Any suggestions are welcome.

Thank you.
Reply all
Reply to author
Forward
0 new messages