Parametric Sort

How can I write a script to get the sort type and flaglist? I mean something like below:

\procedure mylist(sortType, flagList, field:"title")
<$list filter="[tag[TableOfContents]] :sort:<sortType>:<flagList>[get<field>]">

</$list>
\end

Here I want to pass type like: string, number, alphanumeric and flagList like reverse,casesensitive

Looks like this is working:

\procedure mylist(sortType, flagList, field:"title")
<$let
  filtertemplate="[tag[TableOfContents]] :sort:$(sortType)$:$(flagList)$[get[$(field)$]]"
  filterstring={{{ [<filtertemplate>substitute[]] }}}
>
<$list filter=<<filterstring>>>

</$list></$let>
\end

<<mylist "string" "reverse">>
2 Likes

You can use substituted attribute values to make this a little more concise (note the backticks in place of quote marks!)

\procedure mylist(sortType, flagList, field:"title")
<$let
  filtertemplate=`[tag[TableOfContents]] :sort:$(sortType)$:$(flagList)$[get[$(field)$]]`
>
<$list filter=<<filtertemplate>>>

</$list></$let>
\end

And we can technically get even shorter by defining the filter with a macro:

\procedure mylist(sortType, flagList, field:"title")
\define filtertemplate() [tag[TableOfContents]] :sort:$(sortType)$:$(flagList)$[get[$(field)$]]
<$list filter=<<filtertemplate>>>

</$list>
\end
4 Likes

Yay! :clap:

Even shorter: attribute substitution works directly in the list widget

\procedure mylist(sortType, flagList, field:"title")
<$list filter=`[tag[TableOfContents]] :sort:$(sortType)$:$(flagList)$[get[$(field)$]]`>

</$list>
\end
4 Likes

Thank you all! Works like a charm. Now using substitute operator (backticks, and substitute) I can make dynamic filters.

Great solutions by all participants. I am really excited when I see a discussion on TW filter language.

Yet another clean solution using the new terminology in TW 5.3.x (function)

\procedure mylist(filter, sortType, flagList, field:"title")
\function myfilter() "[subfilter<filter>] :sort:$(sortType)$:$(flagList)$[get[$(field)$]]" :map[substitute[]]
<$list filter=<<myfilter>> template="$:/core/ui/ListItemTemplate"/>
\end

<<mylist filter:"[tag[Learning]limit[10]]" sortType:"alphanumeric" flagList:"reverse,caseinsensitive">>

This is equivalent to:

<$list filter="[tag[Learning]limit[10]] :sort:alphanumeric:reverse,caseinsensitive[get[title]]" template="$:/core/ui/ListItemTemplate"/>

NOTE: In <$list filter=<<myfilter>> the myfilter is first rendered and generates the final filter and then $list evaluates it. Don’t use it like a filter operator (I intentionally did not include the dot in the name).

2 Likes