Templater UserScript Sort the Results that are displayed in DropDownList

Hello all,

I have created a Userscript that is called from one of my Templater Templates.

My user script returns an array of items. and my template shows these items in a suggester dropdownlist.

Is there a way I can sort these items in the dropdown?

I tried the normal JS .sort() and a few other ideas I had.

I know I could recreate the array alphabetically but if it gets big that would be a Huge pain!
There should be some way to sort this.

Here is my function.

async function chooseColor(tp) {
	const colors = 
	[
		'orange',
		'yellow', 'yyellow',
		'gray', ' ', '', 'none',
		'blue','bblue','cyan',
		'green', 'ggreen', 'sqlgreen', 'comment',
		'red', 'rred', 'sqlred',
		'purple', 'ppurple',
		'pink', 'ppink', 'sqlpink',
		'vsaction', 'vshelper', 'vspink',
		'gold', 'silver', 'litesilver',
		'brown'
	 ];

	return await tp.system.suggester(colors, colors, false, 'Color');
}

Thank you.

How did you do your .sort() since it didn’t work? At least, here is a variant which works in my setup:

async function chooseColor(tp) {
  const colors = 
	[
		'orange',
		'yellow', 'yyellow',
		'gray', 
		'blue', 'bblue', 'cyan',
		'green', 'ggreen', 'sqlgreen', 'comment',
		'red', 'rred', 'sqlred',
		'purple', 'ppurple',
		'pink', 'ppink', 'sqlpink',
		'vsaction', 'vshelper', 'vspink',
		'gold', 'silver', 'litesilver',
		'brown'
	 ];
     
  return await tp.system.suggester(t => t, ['', ' ', 'none', ...colors.sort()], false, 'Color')
}

module.exports = chooseColor

I’ve done a few slight changes. First of all I’m using t => t, to pick what to show in the suggester menu, whilst the second argument holds the actual list to choose from. The second argument I’ve opted for a combination of the non-color options, and the spread of the sorted colors. This keeps the non-color options at the start, with the other sorted options after that initial setup.

I’m not sure why you’ve got the '', ' ', 'none' options in your list, but I kept them as you had them originally. I reckon I would only use one of them, and then check for that in the code calling this user functions.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.