The Indexes Operator

Does the indexes operator sort the output? Why?

Test

  1. goto https://tiddler.wiki.com
  2. create a dictionary tiddler called test as below
jane: mother
loo : father
ana: dauhther
kian: son
  1. create another tiddler to test with below content
<$list filter="[[test]indexes[]]" variable=link>

* my name: <<link>>

</$list>

It produces

  • my name: ana

  • my name: jane

  • my name: kian

  • my name: loo

But I do not want to sort output internally!

EDIT: I know I can sort using sortby, to retain the original order, but this is infeasible for large data.

EDIT ii: See the workaround for this case: The Indexes Operator - #8 by Mohammad

Speculation but indexing/indexes is usually sorted to support locating the index value.

Workaround, especially with dictionary tiddlers parse the text field directly;

<ul>
<$list filter="[[Data]get[text]splitregexp[\n]]" variable=each-line>
<li> {{{ [<each-line>split[:]first[]] }}}</li>
</$list>
</ul>

Produces

Thank you Tony! Good workaround!

See: $:/core/modules/filters/indexes.js

/*\
title: $:/core/modules/filters/indexes.js
type: application/javascript
module-type: filteroperator

Filter operator for returning the indexes of a data tiddler

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/
exports.indexes = function(source,operator,options) {
	var results = [];
	source(function(tiddler,title) {
		var data = options.wiki.getTiddlerDataCached(title);
		if(data) {
			$tw.utils.pushTop(results,Object.keys(data));
		}
	});
	results.sort();
	return results;
};

})();

Note to results.sort();

FYI: I removed results.sort(); from $:/core/modules/filters/indexes.js saved and reloaded and it worked with your test data and did not sort.

  • may be unreliable?

As just a script kiddie I tried to make my own operator rawindexes but it only returns the data tiddler?

rawindexes.json (812 Bytes)

<$list filter="[[Data]rawindexes[]]" variable=link>

* my name: <<link>>

</$list>

So it needs something else :frowning_face: done!

The original order cannot be guaranteed because internally the dictionary tiddler is implemented as a JSON object. See:

There is some discussion about this issue at GitHub Filter operator `indexes` should not change the order of an array · Issue #2559 · Jermolene/TiddlyWiki5 · GitHub

I’ve updated the docs for the indexes operator to note the sorting behaviour:

2 Likes

Thank you all!
I understood there is a technical reason for sorting the output from indexes.

Workaround

For my use case where I need the original order, I used the solution by @TW_Tones and it works for me

<$list filter="[[test]get[text]splitregexp[\n]] :map[split[:]trim[]]" variable=link>

* my name: <<link>>

</$list>
2 Likes