Sorting Search Results

159 views
Skip to first unread message

michaelha...@gmail.com

unread,
Nov 19, 2018, 3:55:55 PM11/19/18
to TiddlyWiki
I am not sure if my question is answerable, but I'd rather ask a foolish question then make a foolish mistake.

I have been using TiddlyWiki 5 as a journal for several years now, and it has served that purpose brilliantly. However, as my collection of journal tiddlers has expanded, I have found myself with a bit of a sorting issue when it comes to using search.

My journal tiddlers are named with the format dd/mm/yyyy. This is very easy for me to read, and lets me quickly track down dates. When I try to find an item within a journal tiddler though, the naming scheme presents an issue. Any list of search results is sorted numerically, and does not recognize the tiddler title as a date. As such, my search results come out in numerical order, sorted by day, month, and then year, rather than the preferred chronological order, which would sort them by year, month, and then day.

My question is: Is there any way I might be able to manipulate TiddlyWiki's search function so that, when I search, I can get the resulting list of journal tiddlers in chronological order?

I do realize the simplest (if most painful) way to solve this would be to rename my tiddlers with a yyyy/mm/dd format, but I would prefer to leave this as a last resort if no other option is feasible. 

I am open to anything, and any suggestions would be greatly appreciated.

Thank you,

Michael

Mark S.

unread,
Nov 19, 2018, 4:56:40 PM11/19/18
to TiddlyWiki
The best solution would be to add a date field and then use that for sorting.

A solution that might work is to use several nested lists.

<$list variable="year" filter="2015 2016 2017" >
<$list variable="month" filter="01 02 03 04 ... 12">
<$list variable="day" filter="01 02 ... 31">
COMBINE YEAR MONTH DAY AND MATCH AGAINST ALL TIDDLERS
</$list></$list></$list>

This would probably be a little slow, if you have 1000s of tiddlers, because it's effectively creating every possible date in a range and then checking to see if a tiddler matches that date.

Good luck
-- Mark

Mark S.

unread,
Nov 19, 2018, 5:32:55 PM11/19/18
to TiddlyWiki
Here's a more complete version, that ONLY works inside of the TW prerelease (because it needs the range operator). ABSOLUTELY make a backup because if something goes wrong, you will have to wait forever for the screen to update:

\define datematch() ^$(month)$/$(day)$/$(year)$
<$list filter="[range[2010,2018,1]]" variable="year">
<$list filter="[range[0.801,0.812,.001]removeprefix[0.8]]" variable="month">
<$list filter="[range[0.801,0.831,.001]removeprefix[0.8]]" variable="day">
<$list filter="[all[tiddlers]regexp<datematch>]" >

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

That blank space in the middle is deliberate. Without that there is no output. This checks every date from 1/1/2010 to 12/31/2018, including a few dates that don't appear on calendars.

The dangers in this technique are one more reason why it's better to set up a separate date field.

Have fun
-- Mark

Mark S.

unread,
Nov 19, 2018, 5:59:54 PM11/19/18
to TiddlyWiki
Note: This assumes padded, double-digit days and months.

michaelha...@gmail.com

unread,
Nov 19, 2018, 7:01:27 PM11/19/18
to TiddlyWiki
I see. Could I rig something with the created field? All of my journal tiddlers' date of creation matches the date they are titled, and that field is already in place. Is there a way might I be able to use that with the search functionality? 

Thank you.

Mark S.

unread,
Nov 19, 2018, 7:38:44 PM11/19/18
to TiddlyWiki
Oh, you never cheat on your journal entries, like I would ;-)

You can edit $:/core/ui/DefaultSearchResultList and change sort[title] to sort[created] in the two places that it appears. Note that this will sort your most recent tiddlers to the bottom. If you have more than 250 results, then the newest won't appear. This will also affect all your results, even non-journal tiddlers. Also, you need to remember that you have edited this tiddler because it won't upgrade automatically when you do any future upgrades to your TW version.

As always, make backups before trying any of this.

Good luck!
-- Mark

michaelha...@gmail.com

unread,
Nov 19, 2018, 7:52:27 PM11/19/18
to TiddlyWiki
Mark, 

Thank you for your suggestions. Do you know if it would be in anyway possible to create another search box in a separate tiddler?

If I created a new tiddler (or tiddlers) specifically for searching through my journal entries, it seems like I could sidestep most of the difficulties you outlined if I were to try mucking about with the $:/core/ui/DefaultSearchResultList directly.

If that would be possible, of course.

Thank you.

TonyM

unread,
Nov 19, 2018, 8:24:54 PM11/19/18
to TiddlyWiki
Mark/.Michelle,

I recently requested that the New Journal button be updated to include a journal-date field so this could be queried and sorted on including modified if needed since the Journal title is not always easy to sort on as you found.

I suppose for you the horse has bolted, but creating a macro to generate the journal-date from the created date on all journal tiddlers could set you up to use this in the future.

In my own organiser I have a macro to do this, and journals can be created from a date field, with <<new-journal-date item-started>> so I can create a new journal from any date, in which the created date will vary from the journal date.

But I have attached an edited version of the core new Journal button that adds the Journal date field.

Regards
Tony
$__core_ui_Buttons_new-journal.json

michaelha...@gmail.com

unread,
Nov 19, 2018, 8:33:58 PM11/19/18
to TiddlyWiki
Tony,

A journal-specific field? Not sure if there would be enough demand to see it implemented in TiddlyWiki proper, but hey, I certainly would not turn up my nose at it.

Unfortunately I do think I am a little late in my TiddlyWiki's lifespan to try implementing something like that now, but I have been consistent in my journal creation, so I think getting a system working using the created field should (hopefully) be within my means.

Thank you.

TonyM

unread,
Nov 19, 2018, 9:09:20 PM11/19/18
to TiddlyWiki
I think there is sufficient demand if we acknowledge Created and modified dates serve their purpose and they do not map to what a journal date field means.

Since the journal creation is in the core I believe it is somewhat incomplete. The idea would be when someone finds them-self in your situation, the documentation suggests to you that all your journal tiddlers have this field and you would use it, end of story. It is trivial to add another field and update the New Journal button. 

It is always harder to fix something retrospectively when someone can predict your need. 

Regards
Tony

michaelha...@gmail.com

unread,
Nov 20, 2018, 8:16:19 AM11/20/18
to TiddlyWiki
Funnily enough, I found a thread this morning for creating search bars: TW5: just a <<search>> search box.

I have taken the code and added it to my TiddlyWiki, and by changing "$:/temp/search" to "$:/temp/searchjournal", the search bar is now separate from the main search bar on the right. 

Now my question becomes, having created another search bar: is there a way I might be able to create a new or modified "$:/core/ui/DefaultSearchResultList" specifically for "$:/temp/searchjournal"?

Thank you.

Mark S.

unread,
Nov 20, 2018, 1:14:41 PM11/20/18
to TiddlyWiki
You could try hacking Jed's $:/macros/Forums/SearchBar code. Looks pretty easy.

First find $:/core/ui/DefaultSearchResultList and clone it, perhaps as $:/core/ui/DefaultSearchResultList2. Make your sort changes to it.

Then open up $:/macros/Forums/SearchBar from Jed's code, scroll to the bottom where you see the transclusion:

{{$:/core/ui/SearchResults}}

and replace it with

{{$:/core/ui/DefaultSearchResultList2}}

This worked in my quick test. Although it's a bit of a cheat because it ignores a step from the original core which I think was meant to make search engines selectable -- but in this case you're making your own engine selection.

Good luck
-- Mark

michaelha...@gmail.com

unread,
Nov 20, 2018, 4:30:00 PM11/20/18
to TiddlyWiki
Mark,

So I took your suggestion and lifted the code from the macro into my own TiddlyWiki, and ended up with this:

<div class="tc-sidebar-lists">


<$set name="searchTiddler" value="$:/temp/searchjournal">
<div class="tc-search">
<$edit-text tiddler="$:/temp/searchjournal" type="search" tag="input" focus={{$:/config/Search/AutoFocus}} focusPopup=<
<qualify "$:/state/popup/search-dropdown">> class="tc-popup-handle"/>
<$reveal state="$:/temp/searchjournal" type="nomatch" text="">
<$button tooltip={{$:/language/Buttons/AdvancedSearch/Hint}} aria-label={{$:/language/Buttons/AdvancedSearch/Caption}} class="tc-btn-invisible">
<$action-setfield $tiddler="$:/temp/advancedsearch" text={{$:/temp/searchjournal}}/>
<$action-setfield $tiddler="$:/temp/searchjournal" text=""/>
<$action-navigate $to="$:/AdvancedSearch"/>
{{$:/core/images/advanced-search-button}}
</$button>
<$button class="tc-btn-invisible">
<$action-setfield $tiddler="$:/temp/searchjournal" text="" />
{{$:/core/images/close-button}}
</$button>
<$button popup=<
<qualify "$:/state/popup/search-dropdown">> class="tc-btn-invisible">
{{$:/core/images/down-arrow}}
<$list filter="[{$:/temp/searchjournal}minlength{$:/config/Search/MinLength}limit[1]]" variable="listItem">
<$set name="resultCount" value="""<$count filter="[!is[system]search{$(searchTiddler)$}]"/>""">
{{$:/language/Search/Matches}}
</$set>
</$list>
</$button>
</$reveal>
<$reveal state="$:/temp/searchjournal" type="match" text="">
<$button to="$:/AdvancedSearch" tooltip={{$:/language/Buttons/AdvancedSearch/Hint}} aria-label={{$:/language/Buttons/AdvancedSearch/Caption}} class="tc-btn-invisible">
{{$:/core/images/advanced-search-button}}
</$button>
</$reveal>
</div>


<$reveal tag="div" class="tc-block-dropdown-wrapper" state="$:/temp/searchjournal" type="nomatch" text="">


<$reveal tag="div" class="tc-block-dropdown tc-search-drop-down tc-popup-handle" state=<
<qualify "$:/state/popup/search-dropdown">> type="nomatch" text="" default="">


<$list filter="[{$:/temp/searchjournal}minlength{$:/config/Search/MinLength}limit[1]]" emptyMessage="""
<div class="tc-search-results">{{$:/language/Search/Search/TooShort}}</div>""" variable="listItem">


{{$:/core/ui/DefaultSearchResultListDate}}


</$list>


</$reveal>


</$reveal>


</$set>


</div>

As you can see, I named my clone of the results list $:/core/ui/DefaultSearchResultListDate.

I now have a search bar that sorts its results by date created.

But...

Now when I type a term into the original search bar on the right, the results are shown with two tabs at the top. Both are named "List". The first tab sorts the results by title, as they have always been. When I click on the second List tab, the results are sorted by date created. 

Did I enable sortable search results the moment I created the tiddler $:/core/ui/DefaultSearchResultListDate?

Thank you.

Mark S.

unread,
Nov 20, 2018, 5:35:10 PM11/20/18
to TiddlyWiki
The code you're showing worries me a bit, because it isn't a macro. Hopefully you were just doing a quick paste?

The tiddler that you cloned and renamed, needs to have the $:/tags/SearchResults tag removed. That should make the sidebar work OK.

Hopefully that will do it. If not, it might be time to scratch this approach.

Good luck

-- Mark

michaelha...@gmail.com

unread,
Nov 20, 2018, 6:37:35 PM11/20/18
to TiddlyWiki
Mark,

I lifted the code from (and based the set up on) the https://www.ooktech.xyz:8443/Public#JustSearch tiddler. 

I only intend to use this search bar in this single case. If you'll forgive my ignorance, could you explain why not using the code as a macro worries you? 

In other news, yes, removing the tag has gotten the left sidebar search bar back to normal.

Thank you.

Mark S.

unread,
Nov 20, 2018, 7:54:30 PM11/20/18
to TiddlyWiki
Actually, I was worried about the various macro variable substitutions that would be needed if not used as a macro. But I see that you've made all those changes, so it's all good.

-- Mark
Reply all
Reply to author
Forward
0 new messages